Sunday, May 16, 2010

Array Copy with Java 6

@Test
 public void genericArrayCopyOf() {
    Number[] source = { new Double(5.0), new Double(10.0) };
    Number[] target = Arrays.copyOf(source, source.length);
    assertEquals(source, target);
 }
 
 @Test
 public void copyOfWithRange() {
    String[] source = { "0", "1", "2", "3", "4" };
    String[] target = Arrays.copyOfRange(source, 2, 4);
    assertEquals(new String[] { "2", "3" }, target);
 }

 @Test
 public void genericArrayCopyOfWithNewType() {
    Number[] source = { new Double(5.0), new Double(10.0) };
    Double[] target = Arrays.copyOf(source, source.length, Double[].class);
    assertEquals(source, target);
 }

No comments:

Post a Comment