0

I have rather a trivial question on which I am thinking without success.

Suppose we have a snippet of code like this:

int x[] = new int[]{3,4};

List<int[]> testList = new ArrayList<>();

testList.add(x.clone()); 
//testList.add(Arrays.copyOf(x,x.length)); -neither this works

System.out.println(testList);

the code prints: [[I@6442b0a6]

As I understand, in the code above I have passed a pointer of x to the testList. But I wanted to pass the content of x so that testList contain a copy of x as an element?

What's more I want to do it without explicit iteration over x.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    The idiomatic way to declare an array, is `int[] x = { 3, 4}` Don’t use this legacy syntax with `[]` after the variablen name. Further, it’s not enough to put a `<>` at the `ArrayList` constructor. You should declare a type argument for the variable as well, e.g. `List testList = new ArrayList<>();` Then, the compiler will already tell you that `testList.add(x);` does not the intended thing. Or use `List testList = new ArrayList<>();` if it is the intended thing, so we don’t need to guess. You can copy an array as simple as `x.clone()` – Holger Jun 28 '22 at 09:49
  • Please clarify if you want to copy the elements of `x` individually into `testList`, or if you want to add a copy of `x` _itself_ as an element into `testList`. In other words, do you want a `List` or a `List`? The wording in your question could be interpreted either way. – Slaw Jun 28 '22 at 09:51
  • @Slaw I want to have content of ```x``` inside the ```testList``` as an element – Evgeny Kuznetsov Jun 28 '22 at 10:02
  • 1
    Yes, you said that. That's the exact sentence that's unclear. You say, "content of `x`", but you also say, "as an element". The former indicates you want a `List`. But the latter can be interpreted to mean you want a `List` ("**an** element" is singular). – Slaw Jun 28 '22 at 10:04
  • @Mark Rotteveel I see that the question is closed but my task is not to convert int[] into List in Java, I am trying to understand how to add content of an array to a list as an element which is itself an array – Evgeny Kuznetsov Jun 28 '22 at 10:05
  • @Slaw I want to receive the list of arrays at the end – Evgeny Kuznetsov Jun 28 '22 at 10:06
  • 1
    You already did that, so then your problem seems to be one of printing out the array, which is why I added the second duplicate. `[[I@6442b0a6]` is the toString of a list (`[..]`), which contains a single int-array. The `[I@6442b0a6` is the toString() of an array, and is composed of `[I` (this is an `int[]`) and `6442b0a6`, which is the identity hashcode of that array. If you want to print the contents of the array, you need to take additional measures. – Mark Rotteveel Jun 28 '22 at 10:08
  • 2
    You've already added `x` to your list. Pointers "don't exist" in Java. Both the `x` variable and the element in your list are the same array object in memory. The `[I@6442b0a6` output is simply the implicit "to string" conversion used by arrays. Typically, you would use `Arrays#toString(...)` to actually print out the _contents_. But you're printing out a `List`, which means you'll need to build your own `String` to print out instead of relying on `List#toString()`. – Slaw Jun 28 '22 at 10:10
  • 2
    Your code is good, the issue here is that the `toString` method of primitive arrays is not capable of dysplaying their content. So you can't display them as text while passing directly into a `System.out.println`. Try like this `testList.forEach(arr -> System.out.println(Arrays.toString(arr)));` – Roberto Manfreda Jun 28 '22 at 10:13
  • Oh my god, I have killed two days on it, what haven't I tried :D Thank you all very much! – Evgeny Kuznetsov Jun 28 '22 at 10:17

1 Answers1

3

The closest to your code:

Integer[] x = {3, 4};
List<Integer> testList = new ArrayList<>();
Collections.addAll(testList, x); // Size 2.
System.out.println(testList);

As the List contains the Object class Integer the ints must be boxed to Integer.

int[] x = {3, 4};
List<Integer> testList = IntStream.of(x).boxed().collect(Collectors.toList());

After clarification

int[] x = {3, 4};
List<int[]> testList = new ArrayList<>();
testList.add(x);

for (int[] y: testList) {
    System.out.println(Arrays.toString(y));
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138