• 面试题玩数组


    //题目

    {"a1","b1","b2","a3","a2","b3"}
    变成
    {"a","a1","a2","a3","b","b1","b2","b3"}
    且每当有新的元素添加时,如:a4、c1、f1、f2,将数组变成:
    {"a","a1","a2","a3","a4","b","b1","b2","b3","c","c1","f","f1","f2"}
    输出结果必须为list,初始输入为list,实现方法不限,请写出方案




    import java.util.*;

    public class G {

    private static Set<String> addCache = new HashSet<>();

    public static void main(String[] args) {
    List<String> list1 = new ArrayList<>();
    //添加进list
    list1.add("a1");
    list1.add("b1");
    list1.add("b2");
    list1.add("a3");
    list1.add("a2");
    list1.add("b3");
    System.out.println(list1);
    change(list1);
    System.err.println(list1);
    //添加新的数据
    list1.add("a4");
    list1.add("c1");
    list1.add("f1");
    list1.add("f2");
    change(list1);
    System.out.println(list1);
    }

    public static void change(List<String> list){
    list.removeAll(addCache);
    //添加首字母
    for (String s : list) {
    addCache.add(s.charAt(0)+"");
    }
    list.addAll(addCache);
    list.sort(null);
    }

    }
  • 相关阅读:
    Qt之json解析
    ListView
    JAVA的文件操作【转】
    可以随着SeekBar滑块滑动显示的Demo
    Android 之Buletooth
    VC++之运算符重载简单小结
    Android之Audio和Video
    Android之内容提供者ContentProvider的总结
    Android之Intent
    Android之Activity小结
  • 原文地址:https://www.cnblogs.com/bzdofj/p/14190070.html
Copyright © 2020-2023  润新知