Double Array Size

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Double Array Size

This Java example program shows how to double array size.

[edit] Java Source Code

  • Package: example.array
  • File: DoubleArray.java
package example.array;
 
public class DoubleArray {
    public static void main(String args[]) {
	int a1[] = { 1, 2, 3, 4, 5 };
	int a2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	System.out.println("Original size: " + a1.length);
	System.out.println("New size: " + doubleArray(a1).length);
	System.out.println("Original size: " + a2.length);
	System.out.println("New size: " + doubleArray(a2).length);
    }
 
    static int[] doubleArray(int original[]) {
	int length = original.length;
	int newArray[] = new int[length * 2];
	System.arraycopy(original, 0, newArray, 0, length);
	return newArray;
    }
}

[edit] What Result You Can Get

Run the program, you will get:

Original size: 5
New size: 10
Original size: 9
New size: 18

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools