Delete array elements
•
Java
import java.util.ArrayList; public class array_delete { public static void main(String[] args) { method_1(); method_2(); } public static void method_1(){ ArrayList<String> arrayList=new ArrayList<String>(); arrayList.clear(); arrayList.add(0,"第一个元素"); arrayList.add("第二个元素"); arrayList.add("第三个元素"); System.out.println("删除前列表元素:"+arrayList); Object[] array_1=arrayList.toArray(); System.out.print("删除前数组元素:"); printArray(array_1); arrayList.remove(1); arrayList.remove("第一个元素"); System.out.println("删除后列表元素:"+arrayList); Object[] array_2=arrayList.toArray(); System.out.print("删除后数组元素:"); printArray(array_2); } public static void method_2(){ Object[] array={"第一个元素","第二个元素","第三个元素"}; System.out.print("删除前数组元素:"); printArray(array); array[1]=array[2]; Object[] array_1=new Object[2]; System.arraycopy(array,array_1,2); System.out.print("删除后数组元素:"); printArray(array_1); } public static void printArray(Object[] array){ for(int i=0;i<array.length;i++){ if(i!=0){ System.out.print(","); } System.out.print(array[i]); } System.out.println(); } }
The output result is:
List element before deleting: [first element, second element, third element] array element before deleting: first element, list element after deleting the third element: [Third Element] array element after deleting: third element array element before deleting: first element, array element after deleting the third element: first element, array element after deleting the third element: first element, third element
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码