-1

So I have this data

 { { 1,  3, 5, 3, 1 },
   { 3,  5, 6, 5, 1 },
   { 7,  2, 3, 5, 0 },
   { 12, 1, 5, 3, 0 },
   { 20, 6, 3, 6, 1 }, 
   { 20, 7, 4, 7, 1 } }

and i want to save it into some kind of collection, list or set. So if that collection was named List,if i were to type List[0][3] it would reffer to int 4. I tried with

ArrayList<int[]> myNumberList = new ArrayList<int[]>();

but i have trouble putting that data into list

Andremoniy
  • 34,031
  • 20
  • 135
  • 241

3 Answers3

1

The array access operator [] is only applicable to arrays. So you can only create 2-dimensional array.

int a[][] = new int[][]{
        {1, 3, 5, 3, 1},
        {3, 5, 6, 5, 1},
        {7, 2, 3, 5, 0},
        {12, 1, 5, 3, 0},
        {20, 6, 3, 6, 1},
        {20, 7, 4, 7, 1}
};
System.out.println(a[0][3]);

But you can't create any type of collection that can use [] to access it's values.

Yoy can still use List of arrays. But you will have to index first dimension, using get() method

List<int[]> a2 = Arrays.asList(
        new int[]{1, 3, 5, 3, 1},
        new int[]{3, 5, 6, 5, 1},
        new int[]{7, 2, 3, 5, 0},
        new int[]{12, 1, 5, 3, 0},
        new int[]{20, 6, 3, 6, 1},
        new int[]{20, 7, 4, 7, 1}
);

System.out.println(a2.get(0)[3]);
Dzmitry Paulenka
  • 1,879
  • 12
  • 14
  • Ok actually i tried that , but i forgot to add other index,thanks anyways. However I'm still interested into putting that data into somesort of collection. I dont need to access it like a[i][j]. – JelOvoSanIliJava Jan 16 '16 at 22:52
  • In that case you will have to use boxed version of ints - `Integer`. I.e. as suggested by @elliott-frisch – Dzmitry Paulenka Jan 16 '16 at 22:55
0

You could make it an Integer[][] and create a List<List<Integer>>. Something like,

Integer[][] arr = { { 1, 3, 5, 3, 1 }, { 3, 5, 6, 5, 1 }, 
        { 7, 2, 3, 5, 0 }, { 12, 1, 5, 3, 0 }, { 20, 6, 3, 6, 1 }, 
        { 20, 7, 4, 7, 1 } };
System.out.println(Arrays.deepToString(arr));
List<List<Integer>> al = new ArrayList<>();
for (Integer[] inArr : arr) {
    al.add(Arrays.asList(inArr));
}
System.out.println(al);

which outputs (formatted for this post)

[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0], 
                  [12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
[[1, 3, 5, 3, 1], [3, 5, 6, 5, 1], [7, 2, 3, 5, 0], 
                  [12, 1, 5, 3, 0], [20, 6, 3, 6, 1], [20, 7, 4, 7, 1]]
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Hard to answer what do you really need in your particular case. But in general list-equivalent of 2-dimensional array, which, I guess, you are looking for, will be List<List<Integer>> type, and in java-8 you can convert it in such way:

    int a[][] = new int[][]{
            {1, 3, 5, 3, 1},
            {3, 5, 6, 5, 1},
            {7, 2, 3, 5, 0},
            {12, 1, 5, 3, 0},
            {20, 6, 3, 6, 1},
            {20, 7, 4, 7, 1}};

    List<List<Integer>> l2 = new ArrayList<>();
    Stream.of(a).forEach(a1 -> l2.add(Arrays.stream(a1).boxed().collect(Collectors.toList())));
Andremoniy
  • 34,031
  • 20
  • 135
  • 241