什么打印出来的结果是这个呢?
什么打印出来的结果是这个呢?为什么不按照顺序打印 a b c d e 呢???
3526198 d
3526198 a
3526198 d
3526198 b
3526198 e
import java.util.*;
class Test{
public static void main(String[] args){
HashSet hs= new HashSet();
hs.add( "a ");
hs.add( "b ");
hs.add( "c ");
hs.add( "d ");
hs.add( "e ");
Iterator it=hs.iterator();
while(it.hasNext()){
System.out.print(it.hashCode()+ " ");
System.out.println(it.next());
}
}
}
------解决方案--------------------用ArryList或 LinkedList 都可以
代码如下:
import java.util.LinkedList;
public class Print {
private static LinkedList <String> linkedlist;
public static void main(String[] args){
linkedlist = new LinkedList <String> ();
linkedlist.addFirst( "a ");
linkedlist.addFirst( "b ");
linkedlist.addFirst( "c ");
linkedlist.addFirst( "d ");
linkedlist.addFirst( "e ");
while (!linkedlist.isEmpty()){
System.out.println(linkedlist.removeLast());
}
}
}
如果用ArrayList:
import java.util.ArrayList;
public class PrintStr {
private static ArrayList <String> arraylist;
public static void main(String[] args){
arraylist = new ArrayList <String> ();
arraylist.add( "a ");
arraylist.add( "b ");
arraylist.add( "c ");
arraylist.add( "d ");
arraylist.add( "e ");
for(String arr:arraylist){
System.out.println(arr);
}
}
}