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);

Recommended Answers

All 3 Replies

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

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

Yes.. it iterates through the array for you. it's called an "enhanced for loop". Google for details & examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.