Array Reallocation : Array Basics « Collections « Java Tutorial

Java Tutorial
1. Language
2. Data Type
3. Operators
4. Statement Control
5. Class Definition
6. Development
7. Reflection
8. Regular Expressions
9. Collections
10. Thread
11. File
12. Generics
13. I18N
14. Swing
15. Swing Event
16. 2D Graphics
17. SWT
18. SWT 2D Graphics
19. Network
20. Database
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web Services SOA
27. EJB3
28. Spring
29. PDF
30. Email
31. J2ME
32. J2EE Application
33. XML
34. Design Pattern
35. Log
36. Security
37. Apache Common
38. Ant
39. JUnit
Java
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Tutorial » Collections » Array Basics 
9. 3. 13. Array Reallocation

You can make a clone of an array and make the new array have a different size.

public static boolean[] copyOf (boolean[] original, int newLength)

public static byte[] copyOf (byte[] original, int newLength)

public static char[] copyOf (char[] original, int newLength)

public static double[] copyOf (double[] original, int newLength)

public static float [] copyOf (float[] original, int newLength)

public static int[] copyOf (int[] original, int newLength)

public static long[] copyOf (long[] original, int newLength)

public static short[] copyOf (short[] original, int newLength)

public static <T> T[] copyOf (T[] original, int newLength)

public static <T,U> T[] copyOf (U[] original, int newLength,java.lang.Class<? extends T[]> newType)

The last method allows you to upcast each element in the original array to a parent type.

Another method similar to copyOf that is also added to Arrays in Java 6 is copyOfRange.

copyOfRange copies a range of elements to a new array.

Like copyOf, copyOfRange also provides overrides for each Java data type.

Here are their signatures:

public static boolean[] copyOfRange (boolean[] original,int from, int to)

public static byte[] copyOfRange (byte[] original,int from, int to)

public static char[] copyOfRange (char[] original,int from, int to)

public static double[] copyOfRange (double[] original,int from, int to)

public static float[] copyOfRange (float[] original,int from, int to)

public static int[] copyOfRange (int[] original, int from, int to)

public static long[] copyOfRange (long[] original, int from, int to)

public static short[] copyOfRange (short[] original, int from, int to)

public static <T> T[] copyOfRange (T[] original, int from, int to)

public static <T,U> T[] copyOfRange (U[] original, int from,int to, java.lang.Class<? extends T[]> newType)

Array reallocation example

import java.util.Arrays;

public class ArrayReallocationDemo {

  public static void main(String[] args) {
    int[] data1 = new int[] { 1357};

    printArray(data1);
    int[] data2 = Arrays.copyOf(data1, 6);
    data2[511;
    printArray(data2);

    int[] data3 = Arrays.copyOfRange(data1, 210);
    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]
9. 3. Array Basics
9. 3. 1. How to define an Array
9. 3. 2. Initializing array elements by index
9. 3. 3. Alternative Array Declaration Syntax
9. 3. 4. Anonymous arrays are declared similarly to regular arrays
9. 3. 5. An array is a Java object
9. 3. 6. To reference the components of an array
9. 3. 7. The Length of an Array
9. 3. 8. Initializing Arrays
9. 3. 9. Using a for loop to iterate over all the elements and set the values
9. 3. 10. Arrays of Characters
9. 3. 11. Using the Collection-Based for Loop with an Array
9. 3. 12. Changing Array Size
9. 3. 13. Array Reallocation
9. 3. 14. Use System.arraycopy to duplicate array
9. 3. 15. Minimum and maximum number in array
9. 3. 16. Shuffle elements of an array
9. 3. 17. Merge (or add) two arrays into one
9. 3. 18. Circular Buffer
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.