使用for loop-java填充数组时出现问题
我在使用for循环自动填充两个数组时遇到一些问题。 我知道如何手动操作,但这显然不是正确的方法。现在代码如下所示:使用for loop-java填充数组时出现问题,java,arrays,Java,Arrays,我在使用for循环自动填充两个数组时遇到一些问题。 我知道如何手动操作,但这显然不是正确的方法。现在代码如下所示: Scanner scan = new Scanner(System.in); String [] allStockCodes = new String [5]; int [] stockPrices = new int [5]; Stock s0 = new Stock ("TEVA", 100); Stock s1 = new
Scanner scan = new Scanner(System.in);
String [] allStockCodes = new String [5];
int [] stockPrices = new int [5];
Stock s0 = new Stock ("TEVA", 100);
Stock s1 = new Stock ("APPLE", 45);
Stock s2 = new Stock ("GOOGLE", 765);
Stock s3 = new Stock ("IBM", 76);
Stock s4 = new Stock ("MICROSOFT", 436);
allStockCodes[0] = s0.getCode();
allStockCodes[1] = s1.getCode();
allStockCodes[2] = s2.getCode();
allStockCodes[3] = s3.getCode();
allStockCodes[4] = s4.getCode();
stockPrices[0] = s0.getPrice();
stockPrices[1] = s1.getPrice();
stockPrices[2] = s2.getPrice();
stockPrices[3] = s3.getPrice();
stockPrices[4] = s4.getPrice();
正如您所看到的,有两个数组,一个需要用字符串填充,另一个需要用int填充。我使用两个构造函数获取字符串(getCode)和int(getPrice)。
那么如何正确地构建循环呢?是的,显然它看起来不太好。试试这个:
Stock[] temp = new Stock[]{s0,s1,s2,s3,s4};
for(int i=0;i<5;i++){
allStockCodes[i] = temp[i].getCode();
stockPrices[i] = temp[i].getPrice();
}
Stock[]temp=新股票[]{s0、s1、s2、s3、s4};
对于(int i=0;i是的,显然它看起来不太好。试试这个:
Stock[] temp = new Stock[]{s0,s1,s2,s3,s4};
for(int i=0;i<5;i++){
allStockCodes[i] = temp[i].getCode();
stockPrices[i] = temp[i].getPrice();
}
Stock[]temp=新股票[]{s0、s1、s2、s3、s4};
对于(int i=0;i我将通过列表:
List<Stock> stocks = new ArrayList<Stock>();
stocks.add(new Stock ("TEVA", 100));
stocks.add(new Stock ("APPLE", 45));
stocks.add(new Stock ("GOOGLE", 765));
stocks.add(new Stock ("IBM", 76));
stocks.add(new Stock ("MICROSOFT", 436));
List<String> allStockCodesList = new ArrayList<String>();
List<Integer> stockPricesList = new ArrayList<Integer>();
for (Stock entry: stocks) {
allStockCodesList.add(enty.getCode());
stockPricesList.add(entry.getprice());
}
List stocks=new ArrayList();
股票。添加(新股票(“TEVA”,100));
添加(新股票(“苹果”,45));
新增(新股票(“谷歌”,765));
新增(新股票(“IBM”,76));
股票。添加(新股票(“微软”,436));
List allStockCodesList=new ArrayList();
List stockPricesList=新的ArrayList();
用于(股票分录:股票){
allStockCodesList.add(enty.getCode());
stockPricesList.add(entry.getprice());
}
我会浏览以下列表:
List<Stock> stocks = new ArrayList<Stock>();
stocks.add(new Stock ("TEVA", 100));
stocks.add(new Stock ("APPLE", 45));
stocks.add(new Stock ("GOOGLE", 765));
stocks.add(new Stock ("IBM", 76));
stocks.add(new Stock ("MICROSOFT", 436));
List<String> allStockCodesList = new ArrayList<String>();
List<Integer> stockPricesList = new ArrayList<Integer>();
for (Stock entry: stocks) {
allStockCodesList.add(enty.getCode());
stockPricesList.add(entry.getprice());
}
List stocks=new ArrayList();
股票。添加(新股票(“TEVA”,100));
添加(新股票(“苹果”,45));
新增(新股票(“谷歌”,765));
新增(新股票(“IBM”,76));
股票。添加(新股票(“微软”,436));
List allStockCodesList=new ArrayList();
List stockPricesList=新的ArrayList();
用于(股票分录:股票){
allStockCodesList.add(enty.getCode());
stockPricesList.add(entry.getprice());
}
“一些问题”什么问题?“一些问题”什么问题?