I have a piece of code like this:
HashSet<Object> Set = new HashSet<Object>();
//add elements to set 'Set'
Object o = null;
for (Iterator<Object> itr=Set.iterator();itr.hasNext();) {
o = itr.next();
//doing some processing on o
}
I assumed that Object o will point to the Object pointed to by itr. But it is not working as expected. The attributes pointed to by itr.next() is not being copied to o. Can anybody suggest where I am going wrong? And also, is there some useful post on object assignment like
o1 = o2
and what happens at memory level in Java?
Below is my actual code:
What I am doing: I have created a set TSet of elements of type Types.AdjList and now I want to process each successive pair of elements of type Types.AdjList and have assigned iterator value during each iteration to two Types.AdjList variables T1 and T2. But T1 and T2 attributes are not matching what the iterator is having.
P.S. Types.AdjList is a HashMap
for (int i=0; i<numT; i++) {
size = generator.nextInt(10)+1;
T[i] = new Types().new AdjList(size);
}
HashSet<Types.AdjList> TSet = new HashSet<Types.AdjList>();
for (int i=0; i<T.length; i++) {
TSet.add(T[i]);
}
Types.AdjList T1 = null, T2 = null;
for (Iterator<Types.AdjList> itr = TSet.iterator(); itr.hasNext();) {
T1 = itr.next();
if (T2 != null) {
size1 = T1.adj.size();
//size1 is returning 0, though T1.adj has some elements
size2 = T2.adj.size();
//do some processing on T1, T2 based on size1 and size2
}
T2 = T1;
}
Any help will be appreciated.
Thanks,
Somnath