Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编辑程序以将变量分配给数组列表_Java_Arrays_Arraylist_Refactoring - Fatal编程技术网

Java 如何编辑程序以将变量分配给数组列表

Java 如何编辑程序以将变量分配给数组列表,java,arrays,arraylist,refactoring,Java,Arrays,Arraylist,Refactoring,所以我现在正在为学校做一个项目。我的初始程序运行良好,然后我被告知将数组重构为数组列表 Eclipse正在突出显示一个特定的行,错误如下: “赋值的左侧必须是变量” 程序中的其他所有内容似乎都没有其他问题 我尝试了几种不同的方法来解决这个问题,结果遇到了麻烦。我附上了引起我问题的代码部分的副本,我希望这个问题不要太模糊 import java.util.ArrayList; import java.util.concurrent.ThreadLocalRandom; public class

所以我现在正在为学校做一个项目。我的初始程序运行良好,然后我被告知将数组重构为数组列表

Eclipse正在突出显示一个特定的行,错误如下:

“赋值的左侧必须是变量”

程序中的其他所有内容似乎都没有其他问题

我尝试了几种不同的方法来解决这个问题,结果遇到了麻烦。我附上了引起我问题的代码部分的副本,我希望这个问题不要太模糊

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));
        }
    }
}
import java.util.ArrayList;
导入java.util.concurrent.ThreadLocalRandom;
公共类股票价格{
静态最终整数最大库存计数=24;
静态最终整数最小库存价格=10;
静态最终整数最大库存价格=100;
//创建库存对象数组
ArrayList myStocks=新的ArrayList();
股票价格{
char startChar='A';
字符串tmpSymbol=null;
int startPrice=0;
int priceRightNow=0;
对于(int idx=0;idx
myStocks.get(idx)=新数组列表(tmpSymbol、startPrice、, 价格(现在)

这行代码正在从ArrayList中获取一项。没有设置

此外,即使是设置而不是获取,您也在将ArrayList中的一个项指定为另一个ArrayList。。。但是你的ArrayList不是ArrayList中的ArrayList(跟我来)?它是一个ArrayList(股票列表)

如果要添加新股票或更新,您需要执行以下操作:

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

// OR

myStocks.set(idx, new Stock(tmpSymbol, startPrice, priceRightNow); // update at index
myStocks.get(idx)=新数组列表(tmpSymbol、startPrice、, 价格(现在)

这行代码正在从ArrayList中获取一项。没有设置

此外,即使是设置而不是获取,您也在将ArrayList中的一个项指定为另一个ArrayList。。。但是你的ArrayList不是ArrayList中的ArrayList(跟我来)?它是一个ArrayList(股票列表)

如果要添加新股票或更新,您需要执行以下操作:

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

// OR

myStocks.set(idx, new Stock(tmpSymbol, startPrice, priceRightNow); // update at index
get(idx)返回
myStocks
数组列表中
idx
索引处的对象

参考此

也就是说,它基本上返回myStocks数组列表中索引
idx
处对象的实例引用

现在请发表声明

myStocks.get(idx)=新的数组列表(tmpSymbol、startPrice、priceRightNow)

您正在使用
newarraylist(tmpSymbol、startPrice、priceRightNow)返回的对象引用分配引用值,这没有意义

当变量位于表达式的左侧时,可以使用
=

因为,
myStocks.get(idx)
返回索引处对象的引用
idx
(不是实际对象),该对象不是存储
new ArrayList(tmpSymbol,startPrice,priceRightNow)引用的变量。

现在,为了让代码正常工作,因为您正在覆盖索引
idx
处的arraylist引用。使用
set()
将解决此问题

请参阅:

像这样的

myStocks.set(idx,新股票(tmpSymbol,startPrice,priceRightNow))

谢谢。

myStocks.get(idx)返回
myStocks
数组列表中
idx
索引处的对象

参考此

也就是说,它基本上返回myStocks数组列表中索引
idx
处对象的实例引用

现在请发表声明

myStocks.get(idx)=新的数组列表(tmpSymbol、startPrice、priceRightNow)

您正在使用
newarraylist(tmpSymbol、startPrice、priceRightNow)返回的对象引用分配引用值,这没有意义

当变量位于表达式的左侧时,可以使用
=

因为,
myStocks.get(idx)
返回索引处对象的引用
idx
(不是实际对象),该对象不是存储
new ArrayList(tmpSymbol,startPrice,priceRightNow)引用的变量。

现在,为了让代码正常工作,因为您正在覆盖索引
idx
处的arraylist引用。使用
set()
将解决此问题

请参阅:

像这样的

myStocks.set(idx,新股票(tmpSymbol,startPrice,priceRightNow))

谢谢