Confused with the ArrayList

D

dshankhoon

Hello,


ArrayList main = new ArrayList();
ArrayList copy = new ArrayList();
copy.add("Test1");
copy.add("Test2");
main.add(copy);
copy = new ArrayList();
copy.add("Test3");
copy.add("Test4");
main.add(copy);
for(int i=0;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);
for(int j=0;j<temp.size();j++)
{
System.out.println(temp.get(j));
}
}
for(int i=1;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);

temp.set(0,"Test5");
temp.set(1,"Test6");
}
for(int i=0;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);
for(int j=0;j<temp.size();j++)
{
System.out.println(temp.get(j));
}
}
}
 
D

dshankhoon

Sorry Posted before completing the message...

I was expecting an output of
Test1
Test2
Test3
Test4
Test1
Test2
Test3
Test4

But the Actual Output that I got was
Test1
Test2
Test3
Test4
Test1
Test2
Test5
Test6

I am confused with the behaviour on how the new values which has a
scope within the for loop be displayed outside without making any
changes to the main arraylist.
 
C

Chris Dollin

Sorry Posted before completing the message...

I was expecting an output of
Test1
Test2
Test3
Test4
Test1
Test2
Test3
Test4

But the Actual Output that I got was
Test1
Test2
Test3
Test4
Test1
Test2
Test5
Test6

That's what I'd expect.
I am confused with the behaviour on how the new values which has a
scope within the for loop

Eh? Values don't have scope.

You're changing the values stored in the second arraylist in main,
and that's what you're seeing.
be displayed outside without making any
changes to the main arraylist.

You don't update the arraylist referred to by main, but you do
update one of its elements.
 
A

Amit Jain

Hey hi,
can you tell my your expected out is
Test1
Test2
Test3
Test4
Test1
Test2
Test3
Test4

and why not
Test1
Test2
Test3
Test4
Test5
Test6
 
P

Patricia Shanahan

Sorry Posted before completing the message...

I was expecting an output of ....
Test3
Test4

But the Actual Output that I got was ....
Test5
Test6

I am confused with the behaviour on how the new values which has a
scope within the for loop be displayed outside without making any
changes to the main arraylist.

Values do not have scope. Variables have scope, but data structures such
as ArrayList are means of remembering values.

....
Declares a variable "main", and initializes it as a pointer to a new
ArrayList, call this one "main-ArrayList".

Declares a variable "copy", and initializes it with a pointer to a new
ArrayList, call this one "copy-ArrayList".

Makes elements 0 and 1 of copy-ArrayList point to the String objects for
the literals "Test1" and "Test2".

Makes element 0 of main-ArrayList point to copy-ArrayList.

Changes the variable copy to point to a new ArrayList, call this one
copy1-ArrayList.

Makes elements 0 and 1 of copy1-ArrayList point to the String objects
for the literals "Test3" and "Test4".

Makes element 1 of main-ArrayList point to copy1-ArrayList.

Makes temp point to the same object as element i of main.

Print each element of it.

The net effect of all this is to print the elements of copy-ArrayList
followed by the contents of copy1-ArrayList.

This is a single iteration loop, because it starts at i=1 and
main-ArrayList only has 2 elements.

Makes temp point to the same object as element 1 of main, copy1-ArrayList.

Changes the contents of elements 0 and 1 of copy1-ArrayList.

Again print the contents of copy-ArrayList followed by the contents of
copy1-ArrayList.

Patricia
 
K

kaldrenon

Hey hi,
can you tell my your expected out is
Test1
Test2
Test3
Test4
Test1
Test2
Test3
Test4

and why not
Test1
Test2
Test3
Test4
Test5
Test6

If you look at the original code, you see that there are two print
loops. One before the reassignment, one after. They're actually
identical, I think. Each loop goes through the contents of ArrayList
main, each of which is an ArrayList, and prints the elements in those
sub-lists.

Like this:

main(0) => [ArrayList a, ArrayList b]
a => ["Test1","Test2"]
print "Test1"
print "Test2"
b => ["Test3","Test4"]
print "Test3"
print "Test4"

however, after the loop in the middle, the contents of ArrayList b
have changed, so the second time it prints "Test5","Test6" instead.


To the OP:

I think that your confusion has to do with scope and the concept of by
reference versus by value. In the middle loop:

for(int i=1;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);

temp.set(0,"Test5");
temp.set(1,"Test6");
}

The variable temp is a reference to the second ArrayList in the
ArrayList main, if I'm not mistaken. In simple terms, that means that
whatever you do to temp gets reflected in whenever you look at the
second ArrayList in main. Instead of "temp" and "main" being two
separate objects, think of them as two names for the same object.

Also, as a small note on your loop here (Patricia mentioned this too)
- You loop from 1 to < main.size(), but since main.size() == 2, it
runs once, i gets set to 2, and the it quits. Also, since i starts at
1, you won't touch the first element of main. Keep in mind that
indices pretty much always start at 0 in Java (and many other
languages, although there are exceptions).

HTH,
Andrew
 
D

Daniel Pitts

Hello,

ArrayList main = new ArrayList();
ArrayList copy = new ArrayList();
copy.add("Test1");
copy.add("Test2");
main.add(copy);
copy = new ArrayList();
copy.add("Test3");
copy.add("Test4");
main.add(copy);
for(int i=0;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);
for(int j=0;j<temp.size();j++)
{
System.out.println(temp.get(j));
}
}
for(int i=1;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);

temp.set(0,"Test5");
temp.set(1,"Test6");
}
for(int i=0;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);
for(int j=0;j<temp.size();j++)
{
System.out.println(temp.get(j));
}
}
}

A note on style:

You should learn about and use Generics when dealing with collections.

List<List<String>> main = new ArrayList<List<String>>();
List<String> copy = new ArrayList<String>();


That way you no longer need to cast to ArrayList
 
C

cHris.z

And I just want to say that it's nothing about scope, all we can see
is that temp can't be used outside the for for circle.
for(int i=1;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);

temp.set(0,"Test5");
temp.set(1,"Test6");
}
But in java, such as Object obj = (Object)anotherObject; actually,
we just get a reference of anotherObject, that means whatever we do to
obj, anoterObject also was done. So, look back the for for circle,
temp is a reference of main.get(i), temp.set(0,"Test5"); we change
temp, also, (ArrayList)main.get(i), cuz' they two are reference to one
heap address. And end the for for circle, temp out of scope, can not
be used, waiting Garbage Collection.
 
L

Lew

cHris.z said:
And I just want to say that it's nothing about scope, all we can see
is that temp can't be used outside the for for circle.
for(int i=1;i<main.size();i++)
{
ArrayList temp = (ArrayList)main.get(i);

temp.set(0,"Test5");
temp.set(1,"Test6");
}
But in java, such as Object obj = (Object)anotherObject; actually,
we just get a reference of anotherObject, that means whatever we do to
obj, anoterObject also was done. So, look back the for for circle,
temp is a reference of main.get(i), temp.set(0,"Test5"); we change
temp, also, (ArrayList)main.get(i), cuz' they two are reference to one
heap address. And end the for for circle, temp out of scope, can not
be used, waiting Garbage Collection.

Mostly correct, except that variables aren't garbage collected, only objects
and classes are.
 
R

Roedy Green

ArrayList main = new ArrayList();
main is almost a reserved word in Java. It is used for the static
method that starts off an app. Using it for some other purpose is bit
like naming your son Susan and your daughter William and being puzzled
when others get confused.
 
R

Roedy Green


I have converted your snippet to an SSCCE.
see http://mindprod.com/jgloss/sscce.html
I have also added some generics to make it clearer
what you are doing.
I have renamed your temp variables to make it clear
they are distinct.
I have added some labelling of the output.

You did not ask a question. Presumably you were expecting different
output than what you got. To me there were no surprises. Perhaps my
comments will explain what was troubling you.

import java.util.ArrayList;
import static java.lang.System.out;

public class CopyConfusion
{
/**
* main startup method
* @param args not used
*/
public static void main ( String [] args )
{
ArrayList<ArrayList<String>> original = new
ArrayList<ArrayList<String>>();
ArrayList<String> copy = new ArrayList<String>();
copy.add("Test1");
copy.add("Test2");
original.add(copy);
copy = new ArrayList<String>();
copy.add("Test3");
copy.add("Test4");
// this adds the entire ArrayList as a single element, not the
individual Strings.
original.add(copy);

// original contains two ArrayLists at this point, each with two
Strings.
out.println(">>>temp1");
for ( int i=0;i<original.size();i++ )
{
final ArrayList<String> temp1 = original.get(i);
for ( int j=0;j<temp1.size();j++ )
{
out.println(temp1.get(j));
}
}
// output is
// >>>temp1
// Test1
// Test2
// Test3
// Test4

// The following code modifies the ArrayLists in the original.
// Perhaps you were expecting get to hand you a clone rather
// than the actual element.
// Note that the for loop starts at 1 rather than 0.
// This means you will leave original[0] intact.
for ( int i=1;i<original.size();i++ )
{
final ArrayList<String> temp2 = original.get(i);
temp2.set(0,"Test5");
temp2.set(1,"Test6");
}

out.println(">>>temp3");
for ( int i=0;i<original.size();i++ )
{
final ArrayList<String> temp3 = original.get(i);
for ( int j=0;j<temp3.size();j++ )
{
out.println(temp3.get(j));
}
}
// output is
// >>>temp3
// Test1
// Test2
// Test5
// Test6

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top