Java基础——List、Set、Map的简单操作与遍历

2023-04-16 14:37:10 浏览数 (1)

第一种:List 【三种迭代方式】

代码语言:javascript复制
public class ListTest {
    ArrayList<String > list1=new ArrayList<String>();
    public ArrayList<String> addMethod(){
        for (int i = 0; i < 9; i  ) {
            list1.add(i "");
        }
        return list1;
    }
    public ArrayList<String> deleteMethod(){
        list1.remove("5");
        return list1;
    }
    public ArrayList<String> updateMethod(){
        list1.set(1,"5");
        return list1;
    }
    /**  迭代器遍历  */
    public void selectMethod1(){
        Iterator <String>it=list1.iterator();
        while(it.hasNext())
        {
            String string=it.next();
            System.out.print(string "t");
        }
    }
    /** foreach()方法 遍历 */
    public void selectMethod2(){
        for(String s:list1){
            System.out.print(s "t");
        }
    }
    /** for()方法 遍历 */
    public void selectMethod3(){
        for (int i = 0; i < list1.size(); i  ) {
            System.out.print(list1.get(i) "t");
        }
    }

    public static void main(String[] args) {
        ListTest list1=new ListTest();
        System.out.print(list1.addMethod() "t");
        list1.selectMethod1();
        System.out.println();
        System.out.print(list1.deleteMethod() "t");
        list1.selectMethod2();
        System.out.println();
        System.out.print(list1.updateMethod() "t");
        list1.selectMethod3();

    }
}

第二种:Set 【两种迭代方式】

代码语言:javascript复制
public class SetTest {
    HashSet<String> set1=new HashSet<>();
    public HashSet<String> addMethod(){
        set1.add("aaa");
        set1.add("bbb");
        set1.add("ccc");
        return set1;
    }
    public HashSet<String> deleteMethod(){
        set1.remove("aaa");
        return set1;
    }
    public HashSet<String> updateMethod(){
        set1.remove("aaa");
        set1.add("sss");
        return set1;
    }
    /**  迭代器遍历  */
    public void selectMethod1(){
        Iterator<String> it=set1.iterator();
        while(it.hasNext())
        {
            String string=it.next();
            System.out.print(string "t");
        }
    }
    /** foreach方法 遍历 */
    public void selectMethod2(){
        for(String s:set1){
            System.out.print(s "t");
        }
    }
    public static void main(String[] args) {
        SetTest set1=new SetTest();
        System.out.print(set1.addMethod() "t");
        set1.selectMethod1();
        System.out.println();
        System.out.print(set1.deleteMethod() "t");
        set1.selectMethod2();
        System.out.println();
        System.out.print(set1.updateMethod() "t");
        set1.selectMethod1();

    }
}

第三种:Map 【两种迭代方式】

代码语言:javascript复制
public class MapTest {
    Map<String,String> map=new HashMap<String,String>();
    public Map<String,String> addMethod(){
        map.put("001","玛卡巴卡");
        map.put("002","胖不拉几");
        map.put("003","叮叮车");
        return  map;
    }
    public Map<String,String> deleteMethod(){
        map.remove("001");
        return map;
    }
    public Map<String,String> updateMethod(){
        map.remove("002");
        map.put("001","二哈");
        return map;
    }
    /** 迭代器 遍历 */
    public void selectMethod(){
        Iterator <String> it=map.keySet().iterator();
        while(it.hasNext())
        {
            String s1=it.next();
            String name=map.get(s1);
            System.out.println(s1 "t" name);
        }
    }
    public void selectMethod1(){
        /**  foreach()方法 遍历  */
        for (Map.Entry<String, String> entry : map.entrySet()){
            System.out.println(entry.getKey()   "t"   entry.getValue());
        }
    }
    public static void main(String[] args) {
        MapTest map1=new MapTest();
        System.out.print(map1.addMethod() "t");
        map1.selectMethod();
        System.out.println();
        System.out.print(map1.deleteMethod() "t");
        map1.selectMethod();
        System.out.println();
        System.out.print(map1.updateMethod() "t");
        map1.selectMethod1();
    }
}

0 人点赞