好的......所以我正处于一个项目的中间,而且我已经碰到了一堵墙。如果有人可以请解释如何将整数数组添加到整数数组的ArrayList,我将不胜感激。这是在处理,javascript版本更具体。我已经测试过,所有工作都符合' symbols.get(i).add(tempArray)。如果我在该行之前打印tempArray,它会给我#6; 6 10 16 10 16 20',它应该如此。不,它不仅仅是println(符号)声明,我还尝试过> println(" blah");'并且没有出现在输出中,所以在.get行上出了点问题。
size(850,250);
String[] myList = new String[100];
ArrayList<Integer[]> symbols = new ArrayList<Integer[]>();
int[] tempArray = new int[];
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
int j = myList.length();
for(int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
symbols.get(i).add(tempArray);
}
println(symbols);
...我也尝试过以下代替&#39; symbols.get(i).add(tempArray);&#39;
for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i[a]) = tempArray[a];
}
println(symbols);
......我也试过了
for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i) = tempArray[a];
}
println(symbols);
...和
for(int a = 0; a < tempArray.length(); a++)
{
symbols[i][a] = tempArray[a];
}
println(symbols);
我出于猜测和尝试,任何帮助都会受到赞赏。
答案 0 :(得分:1)
首先使用ArrayList上的get方法获取整数数组,然后添加[index]以指定在返回的数组的索引处添加的内容。
symbols.get(i)[a] = tempArray[a];
答案 1 :(得分:1)
有一些错误。关键的一点是,您无法将int[]
添加到期望Integer[]
的列表中。在这里,请参阅代码注释:
void setup()
{
size(850, 250);
String[] myList = new String[100];
ArrayList<Integer[]> symbols = new ArrayList<Integer[]>();
// you can't init an array without a inintial dimension
//int[] tempArray = new int[];
int[] tempArray;
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
//length is not a method... no parenthesis here
//int j = myList.length();
int j = myList.length;
for (int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
// you cant add an int[] to an Integer[] arrayList
// you gotta either change the arraylist type or convert the ints to Integers
// also just use .add(). not get().add() it's a one dimension list
symbols.add(parseArray(tempArray));
println(symbols.get(i) );
println("--");
}
}
//convenience function..
Integer[] parseArray(int[] a) {
Integer[] b = new Integer[a.length];
for (int i = 0; i<a.length; i++) {
b[i] = Integer.valueOf(a[i]);
}
return b;
}
和另一种方式......
void setup()
{
size(850, 250);
String[] myList = new String[100];
ArrayList<int[]> symbols = new ArrayList<int[]>();
// you can't init an array without a inintial dimension
//int[] tempArray = new int[];
int[] tempArray;
String numbers = ("6,10,16,10,16,20\n1,25,21,13,3,15\n6,5,20,6,21,20");
myList = (split(numbers, "\n"));
//length is not a method... no parenthesis here
//int j = myList.length();
int j = myList.length;
for (int i = 0; i<j; i++)
{
tempArray = int(split(myList[i], ','));
symbols.add(tempArray);
println(symbols.get(i) );
println("--");
}
}