I have a "for" loop that is creating a variable each time through the loop. I am attempting to insert the results into an empty array at the index of the "i" in the loop. From the best I can tell it seems I need to create a ArrayList vs. an Array to make this happen.
我有一个“for”循环,每次循环都会创建一个变量。我试图将结果插入到循环中“i”的索引处的空数组中。从最好的我可以告诉它似乎我需要创建一个ArrayList与一个数组来实现这一点。
int varNum = 10;
Array someArr = new Array ();
for (int i = 0; i < 10; i++){
varNum = varNum +i;
someArr[i] = varNum;
}
On the first loop I want the 10 to be inserted in my array at the "0 index", 11 inserted at "1 index", 12 at the "2 Index".
在第一个循环中,我希望将10插入到我的数组中的“0索引”,11插入“1索引”,12插入“2索引”。
**The important part is that the Array is not a set size, because I do not know how many indexes I will need in the array, so I want to add them as I needed.
**重要的是,Array不是一个设置大小,因为我不知道我需要在数组中有多少索引,所以我想根据需要添加它们。
2 个解决方案
#1
0
If you use an ArrayList you can call add like this:
如果您使用ArrayList,可以像这样调用add:
int varNum = 10;
ArrayList<Integer> someArr = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
varNum = varNum + i;
someArr.add(varNum);
}
This will allow you to dynamically fill the ArrayList dependent upon how many values it needs to hold.
这将允许您根据需要保存的值来动态填充ArrayList。
#2
0
Better use an ArrayList then, and use their .add() method
那么最好使用ArrayList,并使用他们的.add()方法
someArr.add(varNum)
#1
0
If you use an ArrayList you can call add like this:
如果您使用ArrayList,可以像这样调用add:
int varNum = 10;
ArrayList<Integer> someArr = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
{
varNum = varNum + i;
someArr.add(varNum);
}
This will allow you to dynamically fill the ArrayList dependent upon how many values it needs to hold.
这将允许您根据需要保存的值来动态填充ArrayList。
#2
0
Better use an ArrayList then, and use their .add() method
那么最好使用ArrayList,并使用他们的.add()方法
someArr.add(varNum)