Create Arrays with New

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Create Arrays with New

This Java example shows how to create arrays with New methods.

[edit] Java Source Code

  • Package: com.bruceeckel
  • File: ArrayNew.java
package com.bruceeckel;
 
//: c04:ArrayNew.java
//Creating arrays with new.
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.
 
import java.util.Random;
 
public class ArrayNew {
    static Random rand = new Random();
 
    public static void main(String[] args) {
	int[] a;
	a = new int[rand.nextInt(20)];
	System.out.println("length of a = " + a.length);
	for (int i = 0; i < a.length; i++)
	    System.out.println("a[" + i + "] = " + a[i]);
    }
}

[edit] What Result You Can Get

Run the program, you will get:


length of a = 11
a[0] = 0
a[1] = 0
a[2] = 0
a[3] = 0
a[4] = 0
a[5] = 0
a[6] = 0
a[7] = 0
a[8] = 0
a[9] = 0
a[10] = 0

[edit] Required External Library 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