dnestoff
11/10/2016 - 11:27 PM

Data Structures in Java

Setting up and using different data structures in Java

// The `char` data type is used to represent single characters. That includes the keys on a keyboard that are used to produce text.

// `char` is short for character and can represent a single character.
// All char values must be enclosed in single quotes, like this: 'G'.

// In Java, all VARIABLES must have a specified data type.

	int myNumber = 42;
	boolean isFun = true; 
	char movieRating = 'A'; 

// Equality OPERATORS do not require that operands share the same ordering. 
  // You can test equality across boolean, char, or int data types. 

// Creating and initializing an ArrayList
  // The `add` method will insert at the first position (0) into the list. 
  
ArrayList<String> olympicSports = new ArrayList<String>();
		olympicSports.add("Archery");
		olympicSports.add("Boxing");

  // iterating through an array
  for (String sport: olympicSports) {
			System.out.println(sport);
		}

// Creating an initializing a hash map
  // below we create a HashMap object called myFriends 
  // stores keys of type `String` and values of type `Integer`
HashMap<String, Integer> myFriends = new HashMap<String, Integer>();

  // new method calls object name, but with '()'
  `new HashMap<String, Integer>();`

  // add to  
	myFriends.put("Beijing", 28);
	myFriends.put("London", 29);
	myFriends.put("Rio de Janeiro", 22);
	
  // length (in this case, 3)
	myFriends.size();
for (int i = 0; i < quizGrades.size(); i++) {

  System.out.println( quizGrades.get(i) );

}

// foreach loop
  // the colon (:) can be read as "in"
for (Integer grade : quizGrades){
    System.out.println(grade);
}