1

Currently I have an arrayList which contains several value pairs. I'm trying to print them in matrix format as shown in the example below. Every odd number is the location in the matrix and the following number is the value. The location goes up as in a counter and if the number doesn't exist in the array a 0 is placed in it's location. Bit tricky to explain.

arraylist contains (1, 10, 2, 90, 4, 9, 7, 2, 11, 4, 14, 45)

Output:

0 10 90 0
9 0  0  2
0 0  0  4
0 0  45 0

I've tried:

int position, value;
int size = 16;

for (int i = 0 ; i < size ; i += 2) {
    position = matrix.get(i);

    if(position == i){
        value = matrix.get(i+1);
        System.out.print(value);
    } else {
        System.out.print("0");

      }

}
user2152251
  • 43
  • 1
  • 5
  • 1
    Why do you want to use a `List`, if you already solved the problem here: [LinkedHashMap printing formatted?](http://stackoverflow.com/questions/15314932/linkedhashmap-printing-formatted) – jlordo Mar 11 '13 at 23:05
  • It's for a project, in which I thought it be better to use a linkedHashMap but since finding out that isn't an option anymore I've got to do it with an arrayList. The LinkedHashMap one was perfect! – user2152251 Mar 11 '13 at 23:07

2 Answers2

1

You want to read numbers in your array not one after one, but two after two. Try this (this is not enough to solve your problem, but this will help) :

for (int i = 0 ; i < size ; i += 2) {
    int position = matrix.get(i);
    int value    = matrix.get(i+1);

    ... // Deal with them
}

To actually fill the matrix with the right values, you should use a Map<Integer, Integer>.

Fabien
  • 12,486
  • 9
  • 44
  • 62
  • Cheers, the problem I seem to be having now is that seeing as the position value only goes up to 10 (only 10 in the arrayList) it gets an out of bounds exception when i goes above 10. I can't move it outside of the loop as it needs to change each time the loop goes around. – user2152251 Mar 11 '13 at 23:25
0

You can increment by more than 1 in a for loop, e.g.

for(int i = 0, size = matrix.size( ); i < size ; i = i+2)
{
    ...
}
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • I've managed to get up to the the new code I put in the OP but now I'm getting a out of bounds exception because the matrix only has 10 values and I need to loop it up to 16? – user2152251 Mar 12 '13 at 11:58
  • @user2152251. You are getting what you asked for, by using a hard coded number. I've changed my example to initialize `size` variable to an actual matrix dimensions. – Alexander Pogrebnyak Mar 12 '13 at 13:52
  • I'm not sure if you misunderstood. Size should be the overall size of the matrix, each odd number in array the location of the following value, as I put in the example? If the location isn't in the array, place a 0 there. But I can't seem to fit the "position = matrix.get(i);" anywhere where it wont throw an exception – user2152251 Mar 12 '13 at 15:42
  • I'm going to look at using a singly linked list as that may be a better approach for what I want to achieve – user2152251 Mar 12 '13 at 16:39