Working with scenes
An enhanced for loop can only be used in an array, or on a collection class that implements the Iterable interface
Syntax format:
For (Variable type variable: An array or collection that needs to be iterated) {}
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import Java.util.LinkedHashMap;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.Map.Entry;
public class Fortest {
public static void Main (string[] args) {
Fortest ft = new Fortest ();
Ft.test1 (); Print Results 123
Ft.test2 (); Print results 1234
Ft.test3 ();
The HashMap used in/**
* Output Result:
* 3=BBB
* 2=BBB
* 1=AAA
*/
Ft.test4 ();
/** Output Result:
* 1=AAA
* 2=BBB
* 3=BBB
*/
Ft.test5 ();
/** Output Result:
* 3=BBB
* 2=BBB
* 1=AAA
*/
Ft.test6 ();
/** Output Result:
* 1=AAA
* 2=BBB
* 3=BBB
*/
}
public void Test1 () {
int arr[]={1,2,3};
for (int num:arr) {
System.out.print (num);
}
}
List Collection
public void Test2 () {
List List = new ArrayList ();
List.add (1);
List.add (2);
List.add (3);
List.add (4);
for (Object obj:list) {
System.out.print (obj);
}
}
The traditional way of map collection two
public void Test3 () {
Map map = new HashMap (); Replace the Linkedhashmap first.
Map.put ("1", "AAA");
Map.put ("2", "BBB");
Map.put ("3", "BBB");
Traditional Way 1
Set set = Map.keyset ();
Iterator It=set.iterator ();
while (It.hasnext ()) {
String key = (string) it.next ();
String value = (string) map.get (key);
System.out.println (key+ "=" +value);
}
}
The traditional way of map collection two
public void Test4 () {
Map map = new Linkedhashmap (); Replace the Linkedhashmap first.
Map.put ("1", "AAA");
Map.put ("2", "BBB");
Map.put ("3", "BBB");
Traditional Way 2
Set set = Map.entryset ();
Iterator It=set.iterator ();
while (It.hasnext ()) {
Map.entry Entry = (Entry) it.next ();
String key = (string) entry.getkey ();
String value = (string) entry.getvalue ();
System.out.println (key+ "=" +value);
}
}
Enhanced For1
public void Test5 () {
Map map = new HashMap (); Replace the Linkedhashmap first.
Map.put ("1", "AAA");
Map.put ("2", "BBB");
Map.put ("3", "BBB");
For (Object Obj:map.keySet ()) {
String key= (string) obj;
String value = (string) map.get (key);
System.out.println (key+ "=" +value);
}
}
The traditional way of map collection two
public void Test6 () {
Map map = new Linkedhashmap (); Replace the Linkedhashmap first.
Map.put ("1", "AAA");
Map.put ("2", "BBB");
Map.put ("3", "BBB");
Traditional Way 2
For (Object Obj:map.entrySet ()) {
Map.entry entry= (Entry) obj;
String key = (string) entry.getkey ();
String value = (string) entry.getvalue ();
System.out.println (key+ "=" +value);
}
}
Enhanced for a little bit of attention to the enhancement for only the data
public void Test () {
int arr[]={1,2,3};
for (int i:arr) {
i=10; is equal to only changing the value of I, the array value is unchanged
}
System.out.println (Arr[0]);
System.out.println (Arr[0]);
System.out.println (Arr[0]);
}
Output results: 1 2 3
}