Five Java Delete array elements and repeating array instance code

Source: Internet
Author: User

Five Java Delete array elements and repeating array instance code

This article from the network collection of Java Delete array elements oh, all kinds of delete array elements have their own method, here not only can delete the array elements, but also can delete duplicate array element instances.

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<string> set=new hashset<string> ();
for (int i=0;i<str.length;i++) {
Set.add (Str[i]);
}
String[] A = (string[]) Set.toarray (new String[set.size ()));
for (int i=0;i<a.length;i++) {
System.out.println (A[i]);
}
}
}

Delete Array Method two

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<integer> numlist = new arraylist<integer> ();
for (int i:nums)
Numlist.add (i);
System.out.println (numlist);

Do delete
Iterator<integer> 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 Delete repeating array element three

String[] a={1,2,3,4,5,6,7,8,9};
String[] b={1,3,6};
for (int i;i<b.length;i++) {
A.remove (B[i])//In fact, there is no remove method, I will not transform
}
return A;

Method Four

String[] Filter () {

ArrayList a=new ArrayList ();
for (int i=0;i<a.length;i++) {
Boolean find=false;
for (int j=0;j<b.length;j++) {
if (A[i].equals (B[j])) {
Find=true;
Break
}
}
if (False==find)
A.add (A[i]);
}

Return A.toarray (New string[]{})
}


Method Five

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};//may change
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N Bsp How to remove elements from a[1 and 3 and 6] into 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 ();
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.