Java/JDK 6/Array

Материал из Java эксперт
Перейти к: навигация, поиск

Array Reallocation

 
import java.util.Arrays;
public class ArrayReallocationDemo {
  public static void main(String[] args) {
    int[] data1 = new int[] { 1, 3, 5, 7, 9 };
    printArray(data1);
    int[] data2 = Arrays.copyOf(data1, 6);
    data2[5] = 11;
    printArray(data2);
    int[] data3 = Arrays.copyOfRange(data1, 2, 10);
    printArray(data3);
  }
  // print array elements
  private static void printArray(int[] data) {
    StringBuilder stringBuilder = new StringBuilder("[");
    for (int i = 0; i < data.length; i++) {
      stringBuilder.append(data[i]);
      if (i < data.length - 1)
        stringBuilder.append(", ");
    }
    stringBuilder.append("]");
    System.out.println(stringBuilder);
  }
}
//[1, 3, 5, 7, 9]
//[1, 3, 5, 7, 9, 11]
//[5, 7, 9, 0, 0, 0, 0, 0]





Using Arrays.copyOf to copy an array

 
import java.util.Arrays;
public class ArrayCopy {
  public static void main(String args[]) {
    System.out.printf("Before (original)\t%s%n", Arrays.toString(args));
    String copy[] = Arrays.copyOf(args, 4);
    System.out.printf("Before (copy)\t\t%s%n", Arrays.toString(copy));
    copy[0] = "A";
    copy[1] = "B";
    copy[2] = "C";
    copy[3] = "D";
    System.out.printf("After (original)\t%s%n", Arrays.toString(args));
    System.out.printf("After (copy)\t\t%s%n", Arrays.toString(copy));
  }
}