Skip to content

Instantly share code, notes, and snippets.

@Deathnerd
Created September 28, 2015 19:45
Show Gist options
  • Save Deathnerd/ce5b005c429c2b7cd1a5 to your computer and use it in GitHub Desktop.
Save Deathnerd/ce5b005c429c2b7cd1a5 to your computer and use it in GitHub Desktop.
An example of inline ArrayList declarations
package com.gilleland.george;
import com.gilleland.george.utils.Utils;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// write your code here
Utils.menu(new ArrayList<String>() {{
add("foo");
add("bar");
}});
}
}
package com.gilleland.george.utils;
import java.util.ArrayList;
import java.util.Scanner;
/**
* Created by Wes Gilleland on 9/28/2015.
*/
public class Utils {
private static Scanner in = new Scanner(System.in);
private static String default_prompt = "Enter your choice: ";
/**
* Display a menu for program navigation with a 0 exit option
* @param choices The choices to display
*/
public static int menu(ArrayList<String> choices){
int i = 1;
for(String choice: choices){
System.out.printf("%d. %s\n", i++, choice);
}
System.out.println("0. Exit");
System.out.println(Utils.default_prompt);
return Utils.in.nextInt();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment