2

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

8
  • Do I need to create Object o = new Object(); before copying ? Commented Mar 28, 2012 at 22:49
  • 3
    Please post a concrete example that illustrates it going wrong. Commented Mar 28, 2012 at 22:49
  • 1
    is that a generic method or would you share your object definition? Commented Mar 28, 2012 at 22:50
  • what processing are you doing with o Object ? Commented Mar 28, 2012 at 22:50
  • Seems fine for me. How are you sure attributes do not match? Commented Mar 28, 2012 at 22:50

4 Answers 4

3

There is nothing wrong with your code, but it could use some cleaning up.

If you apply these "best practice" coding standards:

  • Always declare the abstract type (ie Set not HashSet)
  • Use leading-lowercase names for variables (set not Set)
  • Use "foreach" syntax where possible (dispense with using iterators directly)
  • Declare variables so they have the smallest scope possible (o lived past its use)

you get your code refactored to:

for (Object o : new HashSet<Object>(someSet)) {
    // doing some processing on o
}

or even

for (Object o : someSet) { // Not sure why you wanted to make a new Set
    // doing some processing on o
}
Sign up to request clarification or add additional context in comments.

1 Comment

But the size is returning 0 in my actual code that I edited in my original question
1

Where does t2 get set from? It looks like it's always null to me, and therefor the if statement never gets executed. Put a break point there and see what happens.

2 Comments

I have assigned T2 = T1 in the iterator loop. I want to take two consecutive Types.AdjList as I process through the TSet. I assume that assignment should work.
Not the first time through the loop you haven't. The first time through the loop T2 is null.
0

We don't use iterators in Java anymore:

We use 'in'

ArrayList<String> stringList = new ArrayList<String>();
// initialize it with code (not shown)
for (String s: stringList) {
   System.out.println("the current element is:" + s);
}

1 Comment

Hi SunKing2, welcome to stackoverflow! Please try to be as clear as possible in your answers, the asker won't probably know that "in" is part of the foreach construct, and "in" is not in your code either (as it is the colon between s and stringList). Furthermore, even iterators have their use, e.g. for removing instances, so to say that they are not used anymore is simply wrong. The idea within the answer is fine, but please try to be as concise as possible.
-2

You are not iterating correctly.

you need to do


HashSet set = ...
Iterator _i = set.iterator();
while(_i.hasNext()) {
   Object o = _i.next();
}

start there.

Also, as to your question

o1 = o2

Nothing happens to memory. In java, everything is either a primitive or a class. All classes reside in the heap, so o1 = o2 just copies the pointer to o2 over to the variable o1. If o2 is a primitive, it copies the value (rather than a pointer) into o1

2 Comments

@user1291492 & Bohemian: I guess the problem is not to do with the way I am iterating. To answer, this way of iterating is same as what you have written.
Everything isn't either a primitive or a class. There are objects and references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.