帮朋友弄的联系,暂时寄放在这里
import java.util.*;import static java.lang.System.out;
/**
*
* @author vlinux
*/
public class CollTest {
private List<Integer> list;
public CollTest() {
list = Arrays.asList( 77,55,33,99 );
if( list.remove( new Integer(39) ) == false ) {
out.println("删除元素39失败");
}
int index_31 = list.lastIndexOf(31);
if( index_31 >= 0 ) {
list.set( index_31,31 );
} else {
out.println("替换元素31失败");
}
unSort( list );
sort( list );
reverse( list );
}
/**
* 乱序
*/
private void unSort( List<Integer> aList ) {
out.print("\n乱序输出\t");
showList( aList );
}
/**
* 正序
*/
private void sort( List<Integer> aList ){
out.print("\n顺序输出\t");
List<Integer> list = new ArrayList<Integer>( aList );
Collections.sort( list );
Iterator<Integer> it = list.iterator();
showList( list );
}
/**
* 倒序
*/
private void reverse( List<Integer> aList ) {
out.print("\n倒序输出\t");
List<Integer> list = new ArrayList<Integer>( aList );
Collections.sort( list, new Comparator<Integer>(){
public int compare(Integer o1, Integer o2) {
return o1.compareTo(o2)*-1;
}
});
showList( list );
}
/**
* 输出一个集合
*/
private void showList( List<? extends Object> list ) {
Iterator<? extends Object> it = list.iterator();
while( it.hasNext() ) {
out.print(it.next()+" ");
}
}
public static void main(String... args) {
new CollTest();
}
}