Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Ok... So I am in the middle of a project, and I've hit a bit of a wall. If anyone could please explain how to add an integer array to an ArrayList of integer Arrays, I would greatly appreciate it. This is in processing, the javascript version more specifically. I have tested and everything works right up to 'symbols.get(i).add(tempArray). If I print tempArray right before that line it gives me '6 10 16 10 16 20', as it should. And no, it's not just the println(symbols) statement, I also tried just 'println("blah");'and that did not show up in the output, so something is going awry at the .get line.

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);

... I have also tried the following in place of 'symbols.get(i).add(tempArray);'

for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i[a]) = tempArray[a];
}
println(symbols);

... I have also tried

for(int a = 0; a < tempArray.length(); a++)
{
symbols.get(i) = tempArray[a];
}
println(symbols);

... and

for(int a = 0; a < tempArray.length(); a++)
{
symbols[i][a] = tempArray[a];
}
println(symbols);

I'm out of guesses and tries, any help would be appreciated.

share|improve this question
    
Do you mean println(symbols) is not executed and without any exception? – Jaskey Sep 11 '14 at 2:57

2 Answers 2

First get the Array of integer by using the get method on the ArrayList, then add [index] to specify what to add at which index of the returned Array.

symbols.get(i)[a] = tempArray[a];
share|improve this answer

There are some mistakes. The key one is that you can't add int[] to the list expecting Integer[]. Here, see the comments on the code:

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;
}

and the other way...

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("--");
  }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.