Arrays in Java

Arrays in Java


Array is a container that holds objects that are of similar type.Array length is determined during it creation.Below diagram shows an array of ten objects.


Example of Array in Java 

public class ArrayExample {
    
    public static void main(String args[])
    {
    int [] arrayNumbers;
    arrayNumbers=new int[6];
    arrayNumbers[0]=100;
    arrayNumbers[1]=700;
    arrayNumbers[2]=200;
    arrayNumbers[3]=800;
    arrayNumbers[4]=600;
    arrayNumbers[5]=400;
    
      System.out.println("Element at index 0: "
                           +   arrayNumbers[0]);
    System.out.println("Element at index 1: "
                           +   arrayNumbers[1]);
    System.out.println("Element at index 2: "
                           +   arrayNumbers[2]);
    System.out.println("Element at index 3: "
                           +   arrayNumbers[3]);
    System.out.println("Element at index 4: "
                           +   arrayNumbers[4]);
    
    System.out.println("Element at index 5: "
                           +   arrayNumbers[5]);
    }
    
    
}

Declaring other types of array

Just like other variables array declaration has two components i.e
Array Datatype
Array Name
Array naming should follow naming conventions we discussed earlier.
NB:Array declaration does not create actually array but notifies compiler this variable will hold array data type.Below are other types of array declaration.


byte[] arrayOfBytes; 
short[] 
arrayOfShorts; 
long[] arrayOfLongs; 
float[] arrayOfFloats; 
double[] arrayOfDoubles; 
boolean[] arrayOfBooleans; 
char[] arrayOfChars; 
String[] arrayOfStrings;


Also putting the brackets at the end of array variable name is allowed
i.e int arrayNumbers[] 
  however this is discouraged.


Array creation, Initializing and Accessing it

One way to create array is to use new keyword .eg  
int [] arrayNumbers=new Array[5];
Assigning elements in an array can be done as show below.

    arrayNumbers[0]=100;    arrayNumbers[1]=700;    arrayNumbers[2]=200;    arrayNumbers[3]=800;    arrayNumbers[4]=600;    arrayNumbers[5]=400;

Elements in the array can then be accessed by using their index.
      System.out.println("Element at index 0: "                           +   arrayNumbers[0]);    System.out.println("Element at index 1: "                           +   arrayNumbers[1]);    System.out.println("Element at index 2: "                           +   arrayNumbers[2]);    System.out.println("Element at index 3: "
Alternatively,many programmers consider creating and initializing array at the same time.


int arrayNumbers[]={200,300,400,500,600,700}; 


Creating a multidimensional array  in Java

multidimensional arrays are also known as an array of arrays.They are created using two or more brackets [] [].
Example


public class MultiArrayExample {
    public static void main(String args[])
    {
    
     String[][] marks = {
            {"John ", "Eric ", "Jessica "},
            {"40", "70","80"}
        };
        // Mr. Smith
        System.out.println(marks[0][0] + marks[1][0]);
        // Ms. Jones
        System.out.println(marks[0][1] + marks[1][1]);
         System.out.println(marks[0][2] + marks[1][2]);
         
         System.out.println("Array length is " +marks.length );
    }
    
}

Output

John 40Eric 70Jessica 80
Array length can be determined by
       
         System.out.println("Array length is " +marks.length );

Using arraycopy method 

the arraycopy method can be used to copy  data from one array into another.

Example

public class ArrayCopyMethodExample {
    
    public static void main(String args[])
    {
    char copyFrom[]={'d','r','e','r','i','c','p','f'};
    char[] copyTo = new char[4];
    
       System.arraycopy(copyFrom, 2, copyTo, 0, 4);
         System.out.println(new String(copyTo));
    }
}
Output
run:
eric
BUILD SUCCESSFUL (total time: 0 seconds)



COMMENTS

Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Arrays in Java
Arrays in Java
Arrays in Java
https://4.bp.blogspot.com/-5TemdQ6CZtE/Wl9kOfyyycI/AAAAAAAAKB8/mOcMuhBq0dca7Pw2JjI_GVhS9c3v9P4IQCLcBGAs/s320/objects-tenElementArray.gif
https://4.bp.blogspot.com/-5TemdQ6CZtE/Wl9kOfyyycI/AAAAAAAAKB8/mOcMuhBq0dca7Pw2JjI_GVhS9c3v9P4IQCLcBGAs/s72-c/objects-tenElementArray.gif
Java Codes
https://www.download-all.net/2018/01/arrays-in-java.html
https://www.download-all.net/
https://www.download-all.net/
https://www.download-all.net/2018/01/arrays-in-java.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy