下面的程序反转一个int数组。示例public class Tester { public static void main(String[] args) { int[] numbers = {1,2,3,4,5}; //swap the numbers till the midpoint comes for (int start = 0, end = numbers.length - 1; start
为了简化操作,请将数组转换为列表,然后打印它。示例import java.util.Arrays; import java.util.List; public class Tester { public static void main(String[] args) { Integer[] numbers = {1,2,3,4,5}; List list = Arrays.asList(numbers); System.out.println(list); } }输出[1,2,3,4,5]
下面的程序展示了如何从数组中删除负数。示例import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Tester { public static void main(String[] args) { List objArray = new ArrayList(); objArray.clear(); objArray.add(2); objArray.add(-3); objArray.add(4); System.out.println("Array before removing an element "+objArray); Iterator iterator = objArray.iterator(); while(iterator.hasNext()) { Integer next = iterator.next(); if(next < 0) { iterator.remove(); } } System.out.println("Array after removing an element"+objArray); } }输出Array before removing an element [ 2, -3, 4 ] Array after removing an element [ 2, 4 ]