Senior java for loop

format:

for (variable name Data type: traversed collection (Collection ) or array)

Can only be taken out, can not be added or deleted.

To traverse the collection: You can only get the collection element. But the collection does not operate.

In addition to traversing the iterator also operates remove elements in the collection.

How can also ListIterator collection CRUD operations during traversal.

For traditional and advanced for the difference:

There is a high level for limitations. There must be traversed goal.

It recommended through the array of time, or the use of traditional for. Since the conventional angle may be defined for standard.

public class ForDemo {
    public static void main(String[] args) {
        HashMap<Integer, String> hm = new HashMap<Integer, String>();
        hm.put(1, "a");
        hm.put(2, "b");
        hm.put(3, "c");

        Set<Integer> keySet = hm.keySet();
        for (Integer i : keySet) {
            System.out.println(i + "::" + hm.get(i));
        }

        for (Map.Entry<Integer, String> me : hm.entrySet()) {
            System.out.println(me.getKey() + "::" + me.getValue());
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/hongxiao2020/p/12670805.html