import java.util.LinkedList;
import java.util.Iterator;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
class LinkedListLesson {
public static void printElements( Collection c ) {
Iterator it = c.iterator();
while( it.hasNext() ) {
System.out.println( it.next() );
}
System.out.println();
}
public static void main( String[] args ) {
LinkedList ll = new LinkedList();
ll.add( "John Alex" );
ll.add( "Miller Scott" );
ll.add( "John Anna" );
ll.add( "Johnson Jack" );
ll.add( "Hunter Jeff" );
ll.add( "Williams Serena" );
ll.add( "Williams Uenus" );
System.out.println( "--------------------------------" );
System.out.println( "检索 LinkedList 中的对象" );
System.out.println( "--------------------------------" );
printElements( ll );
System.out.println( "--------------------------------" );
System.out.println( "检索 LinkedList 中的指定对象" );
System.out.println( "--------------------------------" );
//在这里对包含 "John" 字符串的数据进行打印
System.out.println( "--------------------------------" );
System.out.println( "职员列表 ( 之前 ): " );
printElements( ll );
System.out.println( "职员列表 ( 之后 ): " );
Collections.shuffle( ll, new Random() );
printElements( ll );
}
}
我要查询其中包含 "John" 的数据( 即显示下面3条数据 ),将其全部打印出来,请问我该怎么做?
John Alex
John Anna
Johnson Jack