Java基础知识List练习作业—02

Java基础知识List练习作业—02
2.已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位
String[] strs = {“12345”,“67891”,“12347809933”,“98765432102”,“67891”,“12347809933”}。
将该数组里面的所有qq号都存放在LinkedList中,
将list中重复元素删除,将list中所有元素分别用迭代器和增强for循环打印出来。

/*2.已知数组存放一批QQ号码,QQ号码最长为11位,最短为5位
String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"}。
将该数组里面的所有qq号都存放在LinkedList中,
将list中重复元素删除,将list中所有元素分别用迭代器和增强for循环打印出来。*/
package day15;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class HomeWork_002 {

        public static void main(String[] args) {
                List<String> list = new LinkedList<String>();
                String[] strs = {"12345","67891","12347809933","98765432102","67891","12347809933"};
                for(int i = 0;i<strs.length;i++) {
                        list.add(strs[i]);
                }
                System.out.println("原号码:");
                for(String str:list)
                        System.out.print(str+"  ");
                del(list);
                System.out.println("\n增强for循环打印:");
                for(String str:list)
                        System.out.print(str+"  ");
                System.out.println("\n迭代器打印:");
                Iterator iterator = list.iterator();
                while(iterator.hasNext()) {
                        System.out.print(iterator.next()+"  ");
                }
        }

        private static void del(List<String> list) {
                for(int i = 0;i<list.size()-1;i++) {
                        for(int j =i+1 ;j<list.size();j++) {
                                if(list.get(i) .equals(list.get(j)) ) {
                                        list.remove(j);
                                       j--;
                                }
                        }
                }
        }

}

你可能感兴趣的:(JavaSE学习)