找到关于编程的34423 篇文章
17K+ 次浏览
使用 Arrays.copyOfRange() 方法获取子数组。示例:import java.util.Arrays; public class Tester { public static void main(String[] args) { int[] array = new int[] {1, 2, 3, 4, 5}; int[] subArray = Arrays.copyOfRange(array, 0, 2); System.out.println("Array: "); for(int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println("Sub array: "); for(int i = 0; i < subArray.length; i++) { System.out.print(subArray[i] + " "); } } }输出:Array: 1 2 3 4 5 Sub array: 1 2
605 次浏览
下面的程序演示了如何从数组中删除负数。示例: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 ]
251 次浏览
可以创建 Object 类的数组,该数组可以接受任何类型的对象。在对这样的数组进行操作时,可以使用 instanceof 运算符。示例:public class Tester { public static void main(String[] args) { Object[] dataArray = new Object[3]; dataArray[0] = new Integer(0); dataArray[1] = new String("1"); dataArray[2] = new Boolean(false); for(Object data: dataArray) { if(data instanceof Integer) { System.out.println(((Integer) data).intValue()); } if(data instanceof String) { System.out.println(data); } if(data instanceof Boolean) { System.out.println(((Boolean) data).booleanValue()); } } } }输出:0 1 false
374 次浏览
下面的程序将 ArrayList 转换为 String[];示例:import java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List names = new ArrayList(); names.add("A"); names.add("B"); names.add("C"); String[] nameArray = names.toArray(new String[names.size()]); for(String name: nameArray) { System.out.println(name); } } }输出:A B C
308 次浏览
下面的程序将向量转换为字符串数组。示例:import java.util.Vector; public class Tester { public static void main(String[] args) { Vector data = new Vector(); data.add("A"); data.add("B"); data.add("C"); String[] strObjects = data.toArray(new String[data.size()]); for(String obj: strObjects) { System.out.println(obj); } } }
4K+ 次浏览
由于 list.toArray() 返回一个 Object[],可以通过传递 String[] 作为参数将其转换为 String 数组。请参见下面的示例。import java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List data = new ArrayList(); data.add("A"); data.add("B"); data.add("C"); //Object[] objects = data.toArray(); String[] strObjects = data.toArray(new String[0]); for(String obj: strObjects) { System.out.println(obj); } } }输出:A B C
538 次浏览
使用 String(byte[]) 构造函数将 byte[] 转换为 String。示例:public class Tester { public static void main(String[] args) { String test = "I love learning Java"; byte[] bytes = test.getBytes(); String converted = new String(bytes); System.out.println(converted); } }输出:I love learning Java
3K+ 次浏览
只需根据空格分割字符串,然后迭代它即可。请参见下面的示例 - 示例:public class Tester { public static void main(String[] args) { String test = "I love learning Java"; String[] subStrings = test.split(" "); for(String subString: subStrings) { System.out.println(subString); } } }输出:I love learning Java