一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

五款java 删除数组元素与重复数组实例代码

时间:2010-10-20 编辑:简简单单 来源:一聚教程网

五款java 删除数组元素与重复数组实例代码

本文章从网络上收藏了java 删除数组元素哦,各种删除数组元素都有自己的方法,这里不但可以删除数组元素而还可以删除重复的数组元素实例。

package com.lzy;


import java.util.hashset;
import java.util.set;

public class test {

 /**
  * @param args
  */
 public static void main(string[] args) {
  // todo auto-generated method stub
  
    string[] str = {"a","c","b","a","b","d"};
    set set=new hashset();
    for(int i=0;i      set.add(str[i]);
    }
   string[] a =  (string[]) set.toarray(new string[set.size()]);
   for(int i=0;i     system.out.println(a[i]);
   }
   }
}

删除数组方法二

package com.akfucc.zhidao;

import java.util.arraylist;
import java.util.collections;
import java.util.iterator;
import java.util.list;

public class p124876743 {
 public static void main(string[] args) {
  int[] nums = { 1, 2, 3, 3, 3, 3, 4 };
  list numlist = new arraylist();
  for (int i : nums)
   numlist.add(i);
  system.out.println(numlist);

  // 做删除
  iterator it = numlist.iterator();
  int temp = -1;
  if (it.hasnext())
   temp = it.next();
  while (it.hasnext()) {
   int i = it.next();
   if (i == temp) {
    it.remove();
   } else {
    temp = i;
   }
  }
  system.out.println(numlist);
 }
}

java删除重复数组元素三

string[] a={1,2,3,4,5,6,7,8,9};
string[] b={1,3,6};
for(int i;i   a.remove(b[i]);//其实没有remove方法,我就不转型了
}
return a;

方法四

string[] filter(){

 arraylist a=new arraylist();
 for(int i=0;i   boolean find=false;
  for(int j=0;j   if(a[i].equals(b[j])){
  find=true;
  break;
  }
  }
  if(false==find)
  a.add(a[i]);
 }
  
 return a.toarray(new string[]{})
}


方法五

public class test {
    public static void main(string[] args) {
        string[] a = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        int[] b = { 0, 3, 6 };// 可能会变化
        // 怎么移除a[1和3和6]中的元素变为a={1,3,5,6,8,9...}
        string[] a0 = new string[a.length - b.length];

        int currentindex = 0;
        for (int i = 0; i < b.length; i++) {
            for (int j = (i == 0 ? 0 : b[i - 1] + 1); j < b[i]; j++) {
                a0[currentindex++] = a[j];
            }
            p(a0);
        }

        for (int i = b[b.length - 1] + 1; i < a.length; i++) {
            a0[currentindex++] = a[i];
        }
        p(a0);
    }

    private static void p(string[] ss) {
        for (string s : ss) {
            system.out.print(s + "t");
        }
        system.out.println();
    }
}

热门栏目