1
import
java.util.Arrays;
2
3 public class ArrayCopyTest {
4 public static void main(String args[]) {
5 String[] base = { " 1 " , " 2 " , " 3 " , " 4 " , " a " , " b " , " c " , " d " , " ! " , " ~ " , " + " };
6 System.out.println( " base: " + Arrays.toString(base));
7 String[] copy = Arrays.copyOf(base, 4 );
8 System.out.println( " copy: " + Arrays.toString(copy));
9 copy = Arrays.copyOfRange(base, 3 , 6 );
10 System.out.println( " copy from base, Range 3 - 6: "
11 + Arrays.toString(copy));
12 }
13 }
2
3 public class ArrayCopyTest {
4 public static void main(String args[]) {
5 String[] base = { " 1 " , " 2 " , " 3 " , " 4 " , " a " , " b " , " c " , " d " , " ! " , " ~ " , " + " };
6 System.out.println( " base: " + Arrays.toString(base));
7 String[] copy = Arrays.copyOf(base, 4 );
8 System.out.println( " copy: " + Arrays.toString(copy));
9 copy = Arrays.copyOfRange(base, 3 , 6 );
10 System.out.println( " copy from base, Range 3 - 6: "
11 + Arrays.toString(copy));
12 }
13 }
输出
base:[1, 2, 3, 4, a, b, c, d, !, ~, +]
copy:[1, 2, 3, 4]
copy from base, Range 3 - 6:[4, a, b]
Arrays.toString 直接把数组转换成字符串
Arrays.copyOf(base, 4); copy 目标的前几个成员
Arrays.copyOfRange(base, 3, 6); 指定copy的范围
Arrays相当有用.
到最近才发现原来java6提供了很多有用的工具类
后续逐步和大家分享学习心得.