赞
踩
String[] array = {"a","b","c","c","d","e","e","e","a"}; List<String> result = new ArrayList<>(); boolean flag; for(int i=0;i<array.length;i++){ flag = false; for(int j=0;j<result.size();j++){ if(array[i].equals(result.get(j))){ flag = true; break; } } if(!flag){ result.add(array[i]); } } String[] arrayResult = (String[]) result.toArray(new String[result.size()]); System.out.println(Arrays.toString(arrayResult));
先遍历原数组,然后遍历结束集,通过每个数组的元素和结果集中的元素进行比对,若相同则break。若不相同,则存入结果集,两层循环进行遍历得出最终结果。
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> list = new ArrayList<>();
list.add(array[0]);
for(int i=1;i<array.length;i++){
if(list.toString().indexOf(array[i]) == -1){
list.add(array[i]);
}
}
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrayResult));
String[] array = {"a","b","c","c","d","e","e","e","a"};
List<String> list = new ArrayList<>();
for(int i=0;i<array.length;i++){
for(int j=i+1;j<array.length;j++){
if(array[i] == array[j]){
j = ++i;
}
}
list.add(array[i]);
}
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arrayResult));
String[] array = {"a","b","c","c","d","e","e","e","a"};
Arrays.sort(array);
List<String> list = new ArrayList<>();
list.add(array[0]);
for(int i=1;i<array.length;i++){
if(!array[i].equals(list.get(list.size()-1))){
list.add(array[i]);
}
}
String[] arrayResult = (String[]) list.toArray(new String[list.size()]);
String[] array = {"a","b","c","c","d","e","e","e","a"};
Set<String> set = new HashSet<>();
for(int i=0;i<array.length;i++){
set.add(array[i]);
}
String[] arrayResult = (String[]) set.toArray(new String[set.size()]);
System.out.println(Arrays.toString(arrayResult));
加入set方法进行添加,虽然是无序排列,但是也更方便的解决了去重的问题。
public void testList() { List<Integer> list=new ArrayList<Integer>(); list.add(1); list.add(2); list.add(4); list.add(1); list.add(2); list.add(5); list.add(1); List<Integer> listTemp= new ArrayList<Integer>(); Iterator<Integer> it=list.iterator(); while(it.hasNext()){ int a=it.next(); if(listTemp.contains(a)){ it.remove(); } else{ listTemp.add(a); } } for(Integer i:list){ System.out.println(i); } }
利用LinkedHashSet进行转换也可
List list=new ArrayList();
list.add("java1");
list.add("java2");
list.add("java3");
for(Iteratorit = list.iterator(); it.hasNext(); )
{
....
}
这种方式在循环执行过程中会进行数据锁定, 性能稍差, 同时,如果你想在寻欢过程中去掉某个元素,只能调用it.remove方法, 不能使用list.remove方法, 否则一定出现并发访问的错误.
List list=new ArrayList();
list.add("java1");
list.add("java2");
list.add("java3");
for(String data : list)
{
....
}
比Iterator 慢,这种循环方式还有其他限制, 不建议使用它。
List list=new ArrayList();
list.add("java1");
list.add("java2");
list.add("java3");
for(int i=0; i<list.size(); i++)
{
....
}
内部不锁定, 效率最高, 但是当写多线程时要考虑并发操作的问题。
List<String> strList = new ArrayList<String>();
strList.add("aa");
strList.add("bb");
//如果要变成String数组,需要强转类型。
final int size = strList.size();
String[] strs = (String[])strList.toArray(new String[size]);
List<String> strList = new ArrayList<String>();
strList.add("aa");
strList.add("bb");
String[] strs = new String[strList.size()];
推荐用List自带的toArray()方法转string[]数组
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。