zoukankan      html  css  js  c++  java
  • 30.2 案例:ArrayList本身数据可以重复,写出去重的方法

    package day30_HashSet;
    /*
    * ArrayList特点(实现List接口)
        有序、可以重复、可以使用索引
    
    *使用ArrayList实现数据去重
    * */
    import java.util.ArrayList;
    
    public class test {
        public static void main(String[] args) {
            ArrayList<String> arr = new ArrayList<String>();
    
            arr.add("aa");
            arr.add("bb");
            arr.add("cc");
            arr.add("cc");
            arr.add("cc");
            arr.add("cc");
            System.out.println(arr);
    
            int index = 0;
    
            while (index != arr.size()) {
                String s = arr.remove(index);//先删一遍
                System.out.println(arr);
                if(arr.remove(s)) { //再删一遍,如果有重复数据则返回true,加一遍(删两次加一次,一直循环到删一次后下次删没有数据)
                    arr.add(index,s);
                }else {//如果第二次删除时没了则false,进入else循环
                    arr.add(index,s);
                    index++;
                    System.out.println("---"+arr);
                }
            }
            System.out.println(arr);
        }
    }

    输出

  • 相关阅读:
    毕设问题02-index.jsp跳转html问题
    毕设问题01-html中引入公共部分代码
    毕设开篇
    object和大括号自定义对象
    数组js
    function 方法的使用
    JavaScript01
    CSS属性
    听说不能改日期了
    获取时间
  • 原文地址:https://www.cnblogs.com/longesang/p/11270666.html
Copyright © 2011-2022 走看看