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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
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
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Collections Data Structure » ArrayScreenshots 
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[inew 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 = 114793 };
    // 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};
    System.out.println("e.length = " + e.length);

  }
///:~



           
       
Related examples in the same category
1. Custom ArraySet implementationCustom ArraySet implementation
2. Custom ArrayMap implementationCustom ArrayMap implementation
3. Doubling the size of an arrayDoubling the size of an array
4. Using reflection to check array type and lengthUsing reflection to check array type and length
5. Using reflection to create, fill, and display an arrayUsing reflection to create, fill, and display an array
6. ArrayEnumeration classArrayEnumeration class
7. Timing array loop performance
8. Treating an Array as an Enumeration
9. Converting a Collection to an ArrayConverting a Collection to an Array
10. Array 2DArray 2D
11. Can you change the .length of an array
12. Show Two-Dimensional Array of Objects
13. ArrayListDemo done over using an ArrayList
14. Array Hunt game
15. Multi Dimension Array
16. Clone Array
17. Associates keys with valuesAssociates keys with values
18. Arrays of primitives
19. Creating arrays with new
20. Array initialization
21. Creating an array of nonprimitive objects
22. Create multidimension arraysCreate multidimension arrays
23. Initializing Array ValuesInitializing Array Values
24. Creating a Two-Dimensional Array
25. Initializing a Two Dimensional ArrayInitializing a Two Dimensional Array
26. Using the length VariableUsing the length Variable
27. Triangular array
28. Grow arrayGrow array
29. Define array for class Define array for class
30. String array and output to consoleString array and output to console
31. Multiply two matrices
32. Array Of Arrays Demo 2Array Of Arrays Demo 2
33. Array Copy DemoArray Copy Demo
34. Java program to demonstrate multidimensional arraysJava program to demonstrate multidimensional arrays
w___ww._j_a___v_a___2s__.__c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.