Java deletes array elements and deletes repeated array elements

Source: Internet
Author: User

Delete an array by means of list

The code is as follows: Copy code

Private String [] removePaths (String path, String [] srcpaths ){
List <String> list = new ArrayList <String> ();
Int k = srcpaths. length; // original string length
Int I = 0;
While (I <k ){
If (! Srcpaths [I]. equals (path )){
List. add (srcpaths [I]); // you can add it to the list without waiting.
   }
I ++;
  }
String [] temp = new String [list. size ()];
For (int j = 0; j <list. size (); j ++ ){
Temp [j] = list. get (j );
  }
  
Return temp;
  
 }

No compiler, just write it. That's what it means...

The code is as follows: Copy code

Private String [] removePaths (String path, String [] srcpaths ){
List list = Arrays. asList (srcpaths );
List. remove (path );
String [] temp = new String [list. size ()];
Return list. toArray (temp );
}

Delete the same element from the array

Sort the source array by default.

Adjacent identical elements

Then, perform a circular operation to delete the same element.

The code is as follows: Copy code

<Html>
<Body>

<Script type = "text/javascript">

Var source = ["Lin Chong", "Lu Zhishen", "Dai Zong", "Lin Chong", "Shi Qian", "Zhu Gui"];
Var target = new Array ();

Source. sort ();
Target. push (source [0]);

For (var I = 1; I <source. length; I ++)
{
If (source [I]! = Source [i-1])
{
Target. push (source [I]);
}
}

Document. write ('original array: '+ source + "<br/> ");
Document. write ('required array: '+ target );

</Script>

</Body>
</Html>

Add two more instances.

The code is as follows: Copy code
Public static void main (String [] args ){
GetDistinct (new int [] {6, 7, 3, 6, 5, 2, 7, 8 });
    }
Static void getDistinct (int array []) {
Java. util. List list = new java. util. ArrayList ();
For (int I = 0; I <array. length; I ++ ){
If (! List. contains (array [I]) {
List. add (array [I]);
System. out. print (array [I] + "");
    }
    }
    }
Output: 6 7 3 5 2 8

Take a look at the ArrayList contains () method source code:

The code is as follows: Copy code
 
Public boolean contains (Object elem ){
Return indexOf (elem)> = 0;
    }
Public int indexOf (Object elem ){
If (elem = null ){
For (int I = 0; I <size; I ++)
If (elementData [I] = null)
Return I;
} Else {
For (int I = 0; I <size; I ++)
If (elem. equals (elementData [I])
Return I;
    }
Return-1;
    }
Related Article

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.