Created
October 5, 2014 22:56
-
-
Save grahammitchell/c8f0504e340978d73595 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Graham Mitchell's crappy intro to arrays | |
// The book is better than this. | |
public class Arrays | |
{ | |
public static void main( String[] args ) | |
{ | |
// traditional "native" arrays | |
// a: [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] | |
// 0 1 2 3 4 5 6 7 8 9 | |
int x = 10; | |
double y = 5.3; | |
String t = "Bob"; | |
String[] a = new String[10]; | |
a[0] = "VICTORY"; // storing a value | |
a[1] = "EPIC FAIL"; | |
String s = a[0]; // retrieving a value | |
System.out.println( a[1] ); | |
a[0] = "FLAWLESS VICTORY"; // replacing an existing value | |
int[] arr = new int[5]; | |
int i; | |
arr[0] = 0; | |
arr[1] = 0; | |
arr[2] = 0; | |
arr[3] = 0; | |
arr[4] = 0; | |
System.out.println("Array contains: " + arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3] + " " + arr[4] ); | |
// Fill each slot of this array with a random number 1-100 | |
arr[0] = 1 + (int)(Math.random()*100); | |
arr[1] = 1 + (int)(Math.random()*100); | |
arr[2] = 1 + (int)(Math.random()*100); | |
arr[3] = 1 + (int)(Math.random()*100); | |
arr[4] = 1 + (int)(Math.random()*100); | |
// Display them again. | |
System.out.println("Array contains: " + arr[0] + " " + arr[1] + " " + arr[2] + " " + arr[3] + " " + arr[4] ); | |
// First dumb thing. | |
i = 0; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = 1; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = 2; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = 3; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = 4; | |
arr[i] = 1 + (int)(Math.random()*100); | |
// Display them again. | |
System.out.print("Array contains: "); | |
i = 0; | |
System.out.print(arr[i] + " "); | |
i = 1; | |
System.out.print(arr[i] + " "); | |
i = 2; | |
System.out.print(arr[i] + " "); | |
i = 3; | |
System.out.print(arr[i] + " "); | |
i = 4; | |
System.out.print(arr[i] + " "); | |
System.out.println(); | |
// Second dumb thing. | |
i = 0; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = i + 1; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = i + 1; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = i + 1; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i = i + 1; | |
arr[i] = 1 + (int)(Math.random()*100); | |
// Display them again. | |
System.out.print("Array contains: "); | |
i = 0; | |
System.out.print(arr[i] + " "); | |
i = i + 1; | |
System.out.print(arr[i] + " "); | |
i = i + 1; | |
System.out.print(arr[i] + " "); | |
i = i + 1; | |
System.out.print(arr[i] + " "); | |
i = i + 1; | |
System.out.print(arr[i] + " "); | |
System.out.println(); | |
// Third dumb thing. | |
i = 0; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i++; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i++; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i++; | |
arr[i] = 1 + (int)(Math.random()*100); | |
i++; | |
arr[i] = 1 + (int)(Math.random()*100); | |
// Display them again. | |
System.out.print("Array contains: "); | |
i = 0; | |
System.out.print(arr[i] + " "); | |
i++; | |
System.out.print(arr[i] + " "); | |
i++; | |
System.out.print(arr[i] + " "); | |
i++; | |
System.out.print(arr[i] + " "); | |
i++; | |
System.out.print(arr[i] + " "); | |
System.out.println(); | |
// Less dumb way; use a FRIGGIN' LOOP! | |
i = 0; | |
while ( i < 5 ) | |
{ | |
arr[i] = 1 + (int)(Math.random()*100); | |
i++; | |
} | |
// Display them again. | |
System.out.print("Array contains: "); | |
i = 0; | |
while ( i < 5 ) | |
{ | |
System.out.print(arr[i] + " "); | |
i++; | |
} | |
System.out.println(); | |
// Less dumb way #2; use a FRIGGIN' LOOP! | |
i = 0; | |
while ( i < arr.length ) | |
{ | |
arr[i] = 1 + (int)(Math.random()*100); | |
i++; | |
} | |
// Display them again. | |
System.out.print("Array contains: "); | |
i = 0; | |
while ( i < arr.length ) | |
{ | |
System.out.print(arr[i] + " "); | |
i++; | |
} | |
System.out.println(); | |
// Awesome way! "Use the FOR LOOP." | |
for ( i=0 ; i < arr.length ; i++ ) | |
{ | |
arr[i] = 1 + (int)(Math.random()*100); | |
} | |
// Display them again. | |
System.out.print("Array contains: "); | |
for ( i=0 ; i < arr.length ; i++ ) | |
{ | |
System.out.print(arr[i] + " "); | |
} | |
System.out.println(); | |
// Awesome way! "Use the FOR LOOP." | |
for ( i=0 ; i < arr.length ; i++ ) | |
arr[i] = 1 + (int)(Math.random()*100); | |
// Display them again. | |
System.out.print("Array contains: "); | |
for ( i=0 ; i < arr.length ; i++ ) | |
System.out.print(arr[i] + " "); | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment