·  JAVA – Syntax – Variables – Collections – Arrays – One-Dimensional

 

- Info:

  - This tutorial shows basic syntax for working with one-dimensional Arrays.

  - Arrays can't be dymanicly resized. To change array size create new one and transfer elements.

  - If you only give a name to array then only reference is created and not the object.

    Later you have to create object with new keyword.

    Scope of that object is equal to the scope of the reference. This means that if you create object inside some {}

    and put it to reference which exists outside that {}, both will exists outside that {}.

 

- Additional Tutorials:

  - Arrays – Multi Dimensional

 

- Create Array – [] After Array Name:

  int array[];                                   //Declared. Used to define scope.

  int array[] = null;                            //Declared, set to null.

  int array[] = new int[5];                      //Declared, memory reserved, inital values.

  int array[] = new int[ ] {0,1,2,3};            //Declared, memory reserved, elements added.

  int array[] = {0,1,2,3};                       //Declared, memory reserved, elements added.

 

- Create Array – [] After Array Type:

  int[] array;                                   //Declared. Used to define scope.

  int[] array = null;                            //Declared, set to null.

  int[] array = new int[5];                      //Declared, memory reserved, initial values.

  int[] array = new int[ ] {0,1,2,3};            //Declared, memory reserved, elements added.

  int[] array = {0,1,2,3};                       //Declared, memory reserved, elements added.

 

- Create Array – Initial Values:

  boolean[] array = new boolean[5];              //Elements set to initial value false.

  char   [] array = new char   [5];              //Elements set to initial value 0x0000.

  byte   [] array = new byte   [5];              //Elements set to initial value 0.

  short  [] array = new short  [5];              //Elements set to initial value 0.

  int    [] array = new int    [5];              //Elements set to initial value 0.

  long   [] array = new long   [5];              //Elements set to initial value 0.

  float  [] array = new float  [5];              //Elements set to initial value 0.0.

  double [] array = new double [5];              //Elements set to initial value 0.0.

  String [] array = new String [5];              //Object elements are set to initial value null.

 

- Change Array:

  array = null;                                  //Set to null.

  array = new int[5];                            //Memory reserved, inital values.

  array = new int[ ] {0,1,2,3};                  //Memory reserved, elements added.

  array = array2;                                //array gets dimension & all elements from array2.

 

- Add Element:

  array[0] = 5;                                  //Add value 5 at index 0.

  Array.set       (array, 0, 5);                 //Add value 5 at index 0.

  Array.setBoolean(array, 0, true);              //Exception if can't convert to array type.

  Array.setChar   (array, 0, 'A');               //-||-.

  Array.setByte   (array, 0, (byte)  0xFF);      //-||-. Convert from int to byte.

  Array.setShort  (array, 0, (short) 0xFFFF);    //-||-. Convert from int to short.

  Array.setInt    (array, 0, 5);                 //-||-.

  Array.setLong   (array, 0, 5);                 //-||-.

  Array.setFloat  (array, 0, (float) 5.5);       //-||-. Convert from double to float.

  Array.setDouble (array, 0, 5.5);               //-||-.

 

- Get Element:

  int     element = array[0];                    //Get element at index 0.

  int     element = Array.get        (array, 0); //Get element at index=0 from Array array.

 

  boolean element = Array.getBoolean (array, 0); //Exception if can't convert from array to element.

  char    element = Array.getChar    (array, 0); //-||-.

  byte    element = Array.getByte    (array, 0); //-||-.

  short   element = Array.getShort   (array, 0); //-||-.

  int     element = Array.getInt     (array, 0); //-||-.

  long    element = Array.getLong    (array, 0); //-||-.

  float   element = Array.getFloat   (array, 0); //-||-.

  double  element = Array.getDouble  (array, 0); //-||-.

 

- Analyze:

  int     length  = array.length;                //Number of elements of Array array.

  int     length  = Array.getLength(array);      //Number of elements of Array array.