0

Hi. I have these exercises to do, to write the output of some coding given. I can compile it in eclipse but doesn't mean i understand. can someone please explain this code to me. thank you.

int[] arrayA = new int[3]; //this part i know, create array size 3
int[] arrayB = arrayA;//from here i don't really understand. is this like create an array and put the first array   
in it? why would you do that?
arrayB[0] = 19;//from here is to put value in each node
arrayB[1] = 2;
arrayB[2] = 35;
for(int element: arrayA)
System.out.println(element);
2
Contributors
3
Replies
17
Views
10 Months
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • To understand this you need to remember: `int[] arrayA = ...` creates a reference variable `arrayA`that holds a pointer to an array `new int[3]` creates an actuial array of 3 elements so ` int[] arrayA = new int[3];` creates an array and creates a pointer to it. line 2 creates … Read More

1

To understand this you need to remember:

int[] arrayA = ... creates a reference variable arrayAthat holds a pointer to an array
new int[3] creates an actuial array of 3 elements
so int[] arrayA = new int[3]; creates an array and creates a pointer to it.

line 2 creates a reference variable (pointer) called arrayB, but it does NOT create a new array. It sets the pointer to refer to the existing array that was created on line 1

0

Okayy.. what about for(int element: arrayA) ? it "grabs" the elements in the array?

This question has already been answered. Start a new discussion instead.
Please be thoughtful and detailed and be sure to adhere to our posting rules.