How do I edit my program in order to assign a variable to an array list

拈花ヽ惹草 提交于 2020-12-15 06:47:45

问题


so I am currently working on a project for school. My initial program ran fine and then I was told to refactor my array into an array list.

Eclipse is highlighting one specific line with the error that

'The lefthand side of an assignment must be a variable'.

Everything else in the program seems to have no other issues.

I have tried a few different things to fix this problem and have hit a wall. I've attached a copy of the section of code causing me issue, and I hope this question isn't too vague.

import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;

public class StockPrices {

    static final int MAX_STOCK_COUNT = 24;
    static final int MIN_STOCK_PRICE = 10;
    static final int MAX_STOCK_PRICE = 100;

    // Create the array of Stock Objects
    ArrayList<Stock> myStocks = new ArrayList<Stock>();

    public StockPrices() {
        char startChar = 'A';
        String tmpSymbol = null;
        int startPrice = 0;
        int priceRightNow = 0;

        for (int idx = 0; idx < MAX_STOCK_COUNT; ++idx) {
            // Generate stock symbol for testing
            tmpSymbol = "" + (char) (startChar + idx) + (char) (startChar + idx + 1) + (char) (startChar + idx + 2);
            // Generate random data for pricing
            startPrice = ThreadLocalRandom.current().nextInt(MIN_STOCK_PRICE, MAX_STOCK_PRICE + 1);
            priceRightNow = ThreadLocalRandom.current().nextInt(MIN_STOCK_PRICE, MAX_STOCK_PRICE + 1);
            myStocks.get(idx) = new ArrayList <Stock>(tmpSymbol, startPrice, priceRightNow); //The issue is with this line starting with "myStocks"**
            System.out.println(myStocks.get(idx));
        }
    }
}

回答1:


myStocks.get(idx) = new ArrayList (tmpSymbol, startPrice, priceRightNow);

This line of code is getting an item from your ArrayList. Not setting.

Furthermore, even if it was setting instead of getting, you are assigning an item in your ArrayList to be another ArrayList... but your ArrayList<> is not an ArrayList of ArrayLists (follow me)? It is an ArrayList< Stock > (list of Stocks).

If you want to add a new stock, or update, you need to do:

myStocks.add(new Stock(tmpSymbol, startPrice, priceRightNow); // add new

// OR

myStocks.set(idx, new Stock(tmpSymbol, startPrice, priceRightNow); // update at index



回答2:


myStocks.get(idx) returns object at idx index in MyStocks array list.

Refer this https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#get-int-

That is, basically it returns instance reference of object at index idx in myStocks array list.

Now for statement

myStocks.get(idx) = new ArrayList <Stock>(tmpSymbol, startPrice, priceRightNow);

you are assigning reference value with object reference returned by new ArrayList <Stock>(tmpSymbol, startPrice, priceRightNow);, which doesn't make sense.

= can be used when variables are on left hand side of the expression.

Since, myStocks.get(idx) returns reference of object at index idx (not the actual object), which is not a variable to store the reference of new ArrayList<Stock>(tmpSymbol, startPrice, priceRightNow).

Now, for your code to work, since you are overwriting the reference of arraylist at index idx. using set() will solve the problem.

Refer this: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#set-int-E-

something like this

myStocks.set(idx,new Stock(tmpSymbol, startPrice, priceRightNow));

Thanks.



来源:https://stackoverflow.com/questions/59076789/how-do-i-edit-my-program-in-order-to-assign-a-variable-to-an-array-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!