1

so I have an int array, and want to have another array that points to the same values as the original array so that when original array changes the second array would point to to the new value.

I tried with

List<Integer> secondList = new ArrayList<>(Arrays.asList(original array));
secondList = secondList.subList(start,finish);

But I get an error that says that .asList returns - list of type int[] Am I doing something wrong? Or is there any good way to do this?

polyx
  • 73
  • 1
  • 9

2 Answers2

0

If the original array is int[], Arrays.asList will return a List<int[]>. If the original array will be Integer[], Arrays.asList will return a List<Integer>.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

You can just make the second list point to the first with the use of the "=" operator.

For example:

List<Integer> firstList = new ArrayList<Integer>();
List<Integer> secondList = firstList;

firstList.add(1);
System.out.println(secondList.size());

Will print out "1" as the size of the second list as they both "point" to the same location in memory.