Java deletes the array element and the code that deletes the repeating array element _java

Source: Internet
Author: User

Delete array with the help of list

Copy Code code as follows:

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]); No, wait, join the list.
}
i++;
}
string[] temp = new string[list.size ()];
for (int j=0;j<list.size (); j + +) {
TEMP[J] = List.get (j);
}
return temp;
}

No compiler, write casually, probably that means ...

Copy Code code as follows:

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 in an array

First sort the source array by default

Make the same element contiguous

Then loop to delete the same element

Copy Code code as follows:

<body>
<script type= "Text/javascript" >
var Source = ["Lin Chong", "Lu", "Dai Zong", "Lin Chong", "Time Move", "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 (' Desired array: ' + target ');
</script>
</body>

Add two more examples

Copy Code code as follows:

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

Look at the ArrayList contains () method source code:

Copy Code code as follows:

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;
}

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.