SHARE
TWEET

Untitled

a guest Dec 10th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class HelloWorld{
  2.  
  3.      public static void main(String []args){
  4.         System.out.println("Hello World, Arrays");
  5.        
  6.         //Object[] definition = (2 ways to define an array)
  7.        
  8.         //Implicit = {def, def, def, def}
  9.         int[] a = {5, 4, 12, 10};
  10.        
  11.         //Explicit = new int[elements/length]
  12.         int[] b = new int[10];
  13.        
  14.         /*
  15.         The array A has 4 elements, but a index ranging from 0-3
  16.         if we said a[3] = 5; Then the 10 would be replaced with a 5.
  17.        
  18.         the array b has no elements defined, but has space for 10.
  19.         Meaning the index is avaliable from 0-9. We can define a
  20.         point on the array by b[5] = 6;
  21.        
  22.         To redefine a element in an array.
  23.         Array[Index] = Definition;
  24.        
  25.         Array capacity is limited. A can never be modified to hold more than
  26.         4 elements and b can never be modified to hold more than 10 elements.
  27.        
  28.         */
  29.                 b[9] = 5; //defines the 10th element but the 9th index to 5;
  30.                 a[2] = 1; //defines the 3nd element but the 2th index to
  31.                 //Causes exception b[10] = 5; There is no 11th element or the
  32.                 //10th index
  33.                
  34.                
  35.         //looping through an ArrayList
  36.         for (int i = 0; i < a.length; i++) { //i ~ Index
  37.             System.out.println(a[i]);
  38.         }
  39.         //Another way to loop. Limit no built in counter (i)
  40.         for(int i : a) {
  41.             System.out.println(i);
  42.         }
  43.        
  44.         loopThroughArray(a); //Passes the Array A as an argument to the
  45.         //function loopThroughArray to the parameters
  46.        
  47.              }
  48.              
  49.              
  50.         public static void loopThroughArray(int[] array) {
  51.             for(int i : array) {
  52.                 System.out.println("Looping: " + i);
  53.             }
  54.         }
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top