Jump to content

[Java] ArrayList element operation issue

Go to solution Solved by highonquack,

Try this

ArrayList<int[]> x = new ArrayList<int[]>();
x.add(new int[] { 0, 0 });
// breakpoint 1
int[] y = new int[2];
y[0] = x.get(0)[0];
y[1] = x.get(0)[1];
// breakpoint 2

Objects are passed by reference in Java. So when you set y equal to the first array in the list, it passed you a pointer/reference to the array that was inside of the ArrayList.

Ok, so we're all familiar with ArrayList stuff in java, but I've faced an issue with it.

Long-shorlty:

ArrayList<int[]> x = new ArrayList<int[]>();
x.add(new int[] { 0, 0 });
// breakpoint 1
int[] y = new int[2];
y = x.get(0);
y[0] = 1;
y[1] = 2;
// breakpoint 2

The values of this single element of x will be different on breakpoints 1 & 2, obviously.

The issue is: I need to form a list first, then get any element and work with it, but not to have this element changed in original list. Kind 'a wanna deal with this thing.

I hope the explanation is clear.

Thanks.

Link to comment
https://linustechtips.com/topic/568554-java-arraylist-element-operation-issue/
Share on other sites

Link to post
Share on other sites

Try this

ArrayList<int[]> x = new ArrayList<int[]>();
x.add(new int[] { 0, 0 });
// breakpoint 1
int[] y = new int[2];
y[0] = x.get(0)[0];
y[1] = x.get(0)[1];
// breakpoint 2

Objects are passed by reference in Java. So when you set y equal to the first array in the list, it passed you a pointer/reference to the array that was inside of the ArrayList.

Link to post
Share on other sites

9 minutes ago, highonquack said:

Try this


ArrayList<int[]> x = new ArrayList<int[]>();
x.add(new int[] { 0, 0 });
// breakpoint 1
int[] y = new int[2];
y[0] = x.get(0)[0];
y[1] = x.get(0)[1];
// breakpoint 2

Objects are passed by reference in Java. So when you set y equal to the first array in the list, it passed you a pointer/reference to the array that was inside of the ArrayList.

Yeah, that's it.

I realized what the core problem is, but didn't really see any other workaround with it (thought about more elegant way).

So I'm gonna do it that way, thanks.

Link to post
Share on other sites

I would suggest against manually copying the array and instead suggest you use Arrays.copyOf. It internally calls System.arrayCopy which will generally be multiple times faster than the alternative. 

 

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

 

ArrayList<int[]> x = new ArrayList<>();
x.add(new int[] { 0, 0 });

// If you don't know the length of the array.
int[] y = Arrays.copyOf(x.get(0), x.get(0).length);

// Using a known length
int xArrLen = 2;
int[] y = Arrays.copyOf(x.get(0), xArrLen);

 

15" MBP TB

AMD 5800X | Gigabyte Aorus Master | EVGA 2060 KO Ultra | Define 7 || Blade Server: Intel 3570k | GD65 | Corsair C70 | 13TB

Link to post
Share on other sites

57 minutes ago, Blade of Grass said:

I would suggest against manually copying the array and instead suggest you use Arrays.copyOf. It internally calls System.arrayCopy which will generally be multiple times faster than the alternative. 

 

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

 


ArrayList<int[]> x = new ArrayList<>();
x.add(new int[] { 0, 0 });

// If you don't know the length of the array.
int[] y = Arrays.copyOf(x.get(0), x.get(0).length);

// Using a known length
int xArrLen = 2;
int[] y = Arrays.copyOf(x.get(0), xArrLen);

 

Thanks, that's more of what I was looking for. Due to highly probabilistic character of my program, I can't really measure what improvement will be, but it's way more elegant.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×