View RSS Feed

Java Array

Java arrays.

  1. Sorting Arrays

    by , 03-04-2012 at 10:26 AM
    To sort arrays, static methods are provided by the class java.util.Arrays. Kinds of arrays that are supported by it are as following:

    i. Arrays of object types
    ii. Arrays of primitive types

    It is possible to apply the sorting method to range of arrays or some particular part. It is also applicable to the whole array.

    For object type array comparators shall be provided so that to define it to such kind of sorting. Few Array methods are explained ...
    Categories
    Sort
  2. Multidimensional arrays

    by , 03-04-2012 at 10:24 AM
    Array of arrays also consists of multi dimensional arrays. Multi-dimensional variables could be declared by specifying the every extra index. This is done by using square bracket set.
    Java Code:
    For example: 
    int myArray[ ][ ] = new int[5][6] ;
    In case of a multi-dimensional array, memory is allocated by specifying memory for leftmost (1st) dimension.

    Rest of the dimensions can be allocated separately.

    In multi-dimensional array, every array’s ...
  3. Two-Dimensional Arrays

    by , 03-04-2012 at 10:23 AM
    2 dimensional arrays are known to be the array of arrays. Number of array types is present, for example:

    1. Array of Strings
    2. Array of ints
    3. Array of Objects



    Array of int consists of type int[]. Two dimensional array will have int[][]. It might also be known as array of arrays of the ints. Such type of arrays are known as 2D arrays.

    Command:
    int[][] array = new int[5][6];
    Array or variable of type int[][] gets declared. Variable ...
  4. arrays are passed by reference

    by , 03-04-2012 at 10:20 AM
    By reference, array is passed to functions. Or it is passed to original as a pointer. This shows that anything which happens to an array in a function would be affecting the original.

    For understanding an array is passed by reference

    Step 1) In editor, code shall be copied

    Java Code:
    class ArrayDemo {
      
       public static void passByReference(String a[]);
         a[1] = "Changed";
       }
      
       public static
    ...
    Categories
    Arrays
  5. Array Store Exception

    by , 03-04-2012 at 10:18 AM
    If array variable v consists of type A[] (where A is a reference type) then v might hold a reference to any array type B[] instance.( provided that B could be assigned to A).

    Example:
    Java Code:
    class Point { int x, y; }
    class ColoredPoint extends Point { int color; }
    class Test {
    	public static void main(String[] args) {
    		ColoredPoint[] cpa = new ColoredPoint[10];
    		Point[] pa = cpa;
    		System.out.println(pa[1] == null);
    		try
    ...

    Updated 03-04-2012 at 10:21 AM by Java Array

    Categories
    Exception
  6. Class Objects for Arrays

    by , 03-04-2012 at 10:17 AM
    An associated class object is present for every array that is shared to other arrays along with similar component type.An array’s direct superclass is Object. Interfaces java.io.Serializable and Cloneable are implemented by every array type.

    Following example code could better explain this:
    Java Code:
    class Test {
    	public static void main(String[] args) {
    		int[] ia = new int[3];
    		System.out.println(ia.getClass());
    		System.out.println(ia.getClass().getSuperclass());
    ...
    Tags: object array Add / Edit Tags
    Categories
    Object Array
  7. Array Initializers

    by , 03-04-2012 at 10:14 AM
    In a declaration, an array initialize might be specified. It also might be specified as an array creation expression part, that gives certain initial values and creates an array.

    ArrayInitializer:
    { VariableInitializersopt ,opt }

    VariableInitializers:
    VariableInitializer
    VariableInitializers , VariableInitializer

    To make it further clear:

    VariableInitializer:
    Expression
    ArrayInitializer
    ...
    Categories
    Initialize
  8. Array Variables

    by , 03-04-2012 at 10:12 AM
    Reference to object is being held by the array type variable. Declaration of a variable of array type doesn’t allocate space for the array components or create the array object. But, declarator’s initializer part may make an array and a reference which will become variable’s initial value.

    Array length is not considered as a part of its type therefore an array type’s single variable might possess reference to other array of varying length.

    Given below are few examples ...
    Categories
    Arrays
  9. Array Types

    by , 03-04-2012 at 10:10 AM
    To write an array type use element type name with few empty square bracket pairs[]. Depth of array nesting is indicated by the no. of bracket pairs. Length of array is not its type part.

    An array’s element type might be any type whether reference or primitive. Particularly,

    • Arrays along with an interface type, as the component type are allowed. Such array’s elements might use null reference as their value or any type instances which implements interface.
    • Arrays
    ...
    Tags: array types Add / Edit Tags
    Categories
    Arrays
  10. Copying Arrays

    by , 03-04-2012 at 10:08 AM
    An arraycopy method is used by the system class which can efficiently be used to copy one array’s data to other:

    Java Code:
    public static void arraycopy(Object src,
                           int srcPos,
                           Object dest,
                           int destPos,
                           int length)
    The array to copyfrom or the array to copyto is specified by the two object arguments. Three int arguments are used for the specification ...
    Categories
    Copy
  11. Creating, Initializing, and Accessing an Array

    by , 03-04-2012 at 10:05 AM
    One can create an array by the help of the new operator. In ArrayDemo program, next statement allocates the array along with quite enough memory. This is done for 10 integer elements so that to assign array to anArray variable.

    Java Code:
    // create an array of integers
    anArray = new int[10];
    In case if the given statement is missing, an error like this would be printed by compiler and also the compilation would fail.


    To array’s each element, ...
  12. Declaring a Variable to Refer to an Array

    by , 03-04-2012 at 10:02 AM
    Array is declared in the above program along with this line of code.
    Java Code:
    // declares an array of integers
    int[] anArray;
    Array declaration also consists of 2 components, just like other declarations for other types of variables. They are
    • Array’s name
    • Array’s type


    Array’s type is written as type[].

    An array’s name could be anything that you may want provided it follows the conventions. Just like other type variables, array ...
    Tags: refer array Add / Edit Tags
    Categories
    Add
  13. Java Array

    by , 03-04-2012 at 10:00 AM
    Array is that container object which consists of fixed value numbers of one single type. When array is created, establishment of the length of an array takes place. Length is fixed after creation of an array. In Hello world application’s main method, example of array has already been given. Here detailed array section will be given.

    Name:  1.JPG
Views: 410
Size:  10.5 KB
    An array of ten elements


    Every item present in array is known as an element. Numerical index is used ...
    Tags: java array Add / Edit Tags
    Categories
    Add