Initialization and re-assignment of arrays : Array « Collections Data Structure « Java






Initialization and re-assignment of arrays

Initialization and re-assignment of arrays
     
// : c11:ArraySize.java
//Initialization & re-assignment of arrays.
//From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
//www.BruceEckel.com. See copyright notice in CopyRight.txt.

class Weeble {
} // A small mythical creature

public class ArraySize {

  public static void main(String[] args) {
    // Arrays of objects:
    Weeble[] a; // Local uninitialized variable
    Weeble[] b = new Weeble[5]; // Null references
    Weeble[] c = new Weeble[4];
    for (int i = 0; i < c.length; i++)
      if (c[i] == null) // Can test for null reference
        c[i] = new Weeble();
    // Aggregate initialization:
    Weeble[] d = { new Weeble(), new Weeble(), new Weeble() };
    // Dynamic aggregate initialization:
    a = new Weeble[] { new Weeble(), new Weeble() };
    System.out.println("a.length=" + a.length);
    System.out.println("b.length = " + b.length);
    // The references inside the array are
    // automatically initialized to null:
    for (int i = 0; i < b.length; i++)
      System.out.println("b[" + i + "]=" + b[i]);
    System.out.println("c.length = " + c.length);
    System.out.println("d.length = " + d.length);
    a = d;
    System.out.println("a.length = " + a.length);

    // Arrays of primitives:
    int[] e; // Null reference
    int[] f = new int[5];
    int[] g = new int[4];
    for (int i = 0; i < g.length; i++)
      g[i] = i * i;
    int[] h = { 11, 47, 93 };
    // Compile error: variable e not initialized:
    //!System.out.println("e.length=" + e.length);
    System.out.println("f.length = " + f.length);
    // The primitives inside the array are
    // automatically initialized to zero:
    for (int i = 0; i < f.length; i++)
      System.out.println("f[" + i + "]=" + f[i]);
    System.out.println("g.length = " + g.length);
    System.out.println("h.length = " + h.length);
    e = h;
    System.out.println("e.length = " + e.length);
    e = new int[] { 1, 2 };
    System.out.println("e.length = " + e.length);

  }
} ///:~



           
         
    
    
    
    
  








Related examples in the same category

1.Initialize a static array
2.Doubling the size of an arrayDoubling the size of an array
3.Timing array loop performance
4.Array 2DArray 2D
5.Can you change the .length of an array
6.Show Two-Dimensional Array of Objects
7.ArrayListDemo done over using an ArrayList
8.Array Hunt game
9.Multi Dimension Array
10.Clone Array
11.Associates keys with valuesAssociates keys with values
12.Arrays of primitives
13.Creating arrays with new
14.Array initialization
15.Creating an array of nonprimitive objects
16.Create multidimension arraysCreate multidimension arrays
17.Initializing Array ValuesInitializing Array Values
18.Creating a Two-Dimensional Array
19.Initializing a Two Dimensional ArrayInitializing a Two Dimensional Array
20.Using the length VariableUsing the length Variable
21.Triangular array
22.Grow arrayGrow array
23.Define array for class Define array for class
24.String array and output to consoleString array and output to console
25.Multiply two matrices
26.Array Of Arrays Demo 2Array Of Arrays Demo 2
27.Array Copy DemoArray Copy Demo
28.Copying Elements from One Array to Another
29.Java program to demonstrate multidimensional arraysJava program to demonstrate multidimensional arrays
30.Extend the size of an array
31.Copy an array
32.Initialize multidimensional array
33.Get array upperbound
34.To get the number of dimensions
35.Resize an array, System.arraycopy()
36.Dump array content: Convert the array to a List and then convert to String
37.java.utils.Arrays provides ways to dump the content of an array.
38.Dump multi-dimensional arrays
39.Use the new shorthand notation to iterate through an array
40.Create a repeated sequence of character
41.Reverse array elements order
42.Convert array of primitives into array of objects
43.Array Initializers
44.Reinitializes a byte array
45.Reinitializes an int array
46.Sum all elements in the array
47.Sums an array of numbers log(x1)...log(xn)
48.A class to iterate over all permutations of an array.
49.Palidrome Array
50.Set of utilities used to manipulate arrays.
51.ArrayUtils provides static methods for manipulating arrays when using a tool such as java.util.ArrayList is inconvenient.
52.Array Util
53.clone two dimensional array