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