Skip to content

Instantly share code, notes, and snippets.

@dhust
dhust / ifMenu.java
Last active September 15, 2016 20:52
Menu Example
public static void main(String[] args) {
// Variables
Scanner input = new Scanner(System.in);
byte userChoice;
// Menu
System.out.println("MENU");
System.out.println("Choose 1 for Dave");
System.out.println("Choose 2 for Melissa");
@dhust
dhust / methodBasic.java
Last active May 20, 2017 00:39
An example of a basic method
public static void main(String[] args) {
// Three "calls" to the method 'printStuff'
printStuff();
printStuff();
printStuff();
}
// This code only runs if it's "called"
public static void printStuff () {
@dhust
dhust / arrayList.java
Created August 3, 2013 00:57
A basic example of an ArrayList. Along with adding and removing Strings.
public static void main(String[] args) {
ArrayList <String> people = new ArrayList < String> ();
people.add("Dave");
people.add("Andrew");
people.add("John");
for (int i=0; i<people.size(); i++) {
System.out.println(people.get(i));
@dhust
dhust / associativeArrays.java
Last active December 20, 2015 14:09
An example of associative arrays. That means there are two arrays where the elements are related (associated) to one another.
public static void main(String[] args) {
String[] artist = {"Snoop Dogg", "Justin Bieber", "Hoodie Allen", "Taylor Swift" };
int[] ages = { 41, 18, 23, 22 };
for (int i=0; i<artist.length; i++) {
System.out.println(artist[i] + " is " + ages[i] + " years old.");
}
/***** Declaring an array of 10 Strings *****/
String[] myList = new String[10];
// prints the second element of the array, since it's [1]
System.out.println( myList[1] );
/***** Declaring and initializing an array of 10 Strings *****/
String[] myList = { null, null, null, null, null, null, null, null, null, null };
// prints the second element of the array
System.out.println(myList[1]);
/***** Declaring an array of 10 Strings *****/
String[] myList = new String[10];
// prints the second element of the array, since it's [1]
System.out.println( myList[1] );
/***** Declaring and initializing an array of 10 Strings *****/
String[] myList = { null, null, null, null, null, null, null, null, null, null };
// prints the second element of the array
System.out.println(myList[1]);
/***** Declaring an array with a set size. The array is automatically filled with zeros. *****/
int[] myList = new int[10];
// prints the first element of the array, since it's [0]
System.out.println( myList[0] );
/***** Declaring & initializing the array with zeros *****/
int[] myList = { 0,0,0,0,0,0,0,0,0,0 };
// prints the first element of the array
System.out.println(myList[0]);
@dhust
dhust / forEach.java
Created August 1, 2013 21:15
A basic for-each loop (iteration)
public static void main(String[] args) {
// Make an array to hold people's names
ArrayList <String> people = new ArrayList < String> ();
// Add people's names to the array
people.add("Dave");
people.add("Andrew");
people.add("John");
@dhust
dhust / forEach.java
Last active December 20, 2015 12:59
A generic for-each loop (iteration)
public static void main(String[] args) {
for (declare a variable : array to iterate through) {
}
}
@dhust
dhust / for.java
Last active December 20, 2015 12:59
Basic example of a for loop with an array
public static void main(String[] args) {
// Make an array
ArrayList <String> people = new ArrayList <String> ();
// Add some people to it
people.add("Dave");
people.add("Andrew");
people.add("John");