Advertisement
therrontelford

Arrays creating

Jan 15th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1.  
  2. public class creatingArrays {
  3.  
  4.     public static void main(String[] args) throws Exception {
  5.         String[] array1;
  6.         array1 = new String[4];
  7.         array1[0] ="Ginger Sling";
  8.         array1[1] ="Creme Tangerine";
  9.         array1[2] ="Savoy Truffle";
  10.         array1[3] ="Pineapple Tart";
  11.         System.out.println(array1[2] +" is in index 2");
  12.        
  13.         // OR
  14.        
  15.         Double[] array2  = new Double[6];
  16.         array2[0] = 3.2;
  17.         array2[1] = 8.1;
  18.         array2[2] = 6.8;
  19.         array2[3] = 7.2;
  20.         array2[4] = 1.8;
  21.         array2[5] = 6.3;
  22.         System.out.println(array2[3]+ " is in index 3");
  23.         //OR
  24.        
  25.         String[] array3 = {"Ginger Sling", "Creme Tangerine", "Savoy Truffle", "Pineapple Tart"};
  26.         System.out.println(array3[2]+ " is in index 2");
  27.        
  28.         // OR
  29.        
  30.         int[] array4 = {9, 15, 4, 8, 9};
  31.         System.out.println(array4[0]+ " is in index 0");
  32.         System.out.println(array4[6] + "does not exist");  // runtime error
  33.  
  34.     }
  35.  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement