simple array
list of five integers:
int[] listOfNumbers = new int[]{54, 34, 53, 74, 12};which is the same as:
int[] listOfNumbers = new int[5]; listOfNumbers[0] = 54; listOfNumbers[1] = 34; listOfNumbers[2] = 53; listOfNumbers[3] = 74; listOfNumbers[4] = 12;list of five words:
String[] listOfWords = new String[]{"yolo","swag","meme","rekt","mlg"};so the following prints the word "meme" to the screen:
System.out.println(listOfWords[2]);
looping through arrays
it is easier to process arrays using loops. also we can use an integer variable to set the array length.//save as loopArray.java import java.util.*; public class loopArray { public static void main(String[] args) { //ask user for size of array: System.out.println("input array length:"); Scanner myInput = new Scanner(System.in); int arrayLength = Integer.valueOf(myInput.nextLine()); //uses arrayLength variable to set length of array: Double[] listOfTransactions = new Double[arrayLength]; //input all the elements into the array: for(int arrayIndex=0; arrayIndex < arrayLength; arrayIndex++) { System.out.println("input element number "+ arrayIndex +":"); listOfTransactions[arrayIndex] = Double.valueOf(myInput.nextLine()); } //read out all the elements stored in the array: for(int arrayIndex=0; arrayIndex < arrayLength; arrayIndex++) { System.out.println(listOfTransactions[arrayIndex]); } } }