• jQuery
  • PHP
  • Core Java
  • SQL Tutorial
  • Java IO
  • Struts
  • JavaScript
  • Servlet
  • RubyonRails

  • <

    Java Array Tutorial

    An array is a data structure in Java, that can store a fixed-size sequential collection of elements of the same data type.Array hold values of homogenous type.

    An array type can be either primitive or reference. Array starts from zero to size-1.


    Java Array Declaration



    String[] Array1 = new String[2];
    String[] Array2 = {"a","b","c", "d", "e"};
    

    How to Initialize arrays in Java

    In this way we can Initialize an array in java.

    
    String[] name = {'arun', 'varun', 'sunil', 'devi'};
    
    

    How to access array elements

    Java array elements can be accessed by using indexing.

    int[] arr = {"10", "20", "30", "40"};
    
    arr[0];     //first element of array 10
    arr[3];     //last element of array:40
    
    

    Length of an Array in Java

    It return length of an array.length field is used to find length of an array.

    int[] array = new int[10]; 
    
     System.out.println("the array is "
                               + array.length); 
    

    Java Multi-Dimensional Array


    Multiple rows and multiple columns in multi-dimensional array, which can have only one full row or one full column.


    Java Multi-Dimensional Array Example



    class MultidimensionalArray { 
       public static void main(String[] args) { 
    
          int[][] a = { 
                {1, 2, 3},  
                {4, 5, 6, 9},  
                {7},  
          }; 
           
          System.out.println("Length of row 1: " + a[0].length); 
          System.out.println("Length of row 2: " + a[1].length); 
          System.out.println("Length of row 3: " + a[2].length); 
       } 
    } 
    

    Output:
    Length of row 1: 3
    Length of row 2: 4
    Length of row 3: 1
    


    Java Reverse an array


    
    import java.util.*; 
      
    public class reversingArray { 
      
        /*function reverses the elements of the array*/
        static void reverse(Integer a[]) 
        { 
            Collections.reverse(Arrays.asList(a)); 
            System.out.println(Arrays.asList(a)); 
        } 
      
        public static void main(String[] args) 
        { 
            Integer [] arr = {1, 2, 3, 4}; 
            reverse(arr); 
        } 
    } 
    

    Java Jagged Array

    Jagged means to have an uneven edge or surface. In java, a jagged array means to have a multi-dimensional array with uneven size of rows in it.















    © copyright 2017-2022 Completedone pvt ltd.