Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.HashMap;
- public class Main {
- public static void main(String[] args) {
- //creatinga new ArrayList object
- ArrayList<Integer> test1 = new ArrayList<Integer>();
- //Using a Fir Loop to add values to th ArrayList
- for (int i = 0 ; i < 5 ; i++){
- test1.add(1);
- }
- //Printing the ArrayList
- for (int i = 0 ; i < 5 ; i++){
- System.out.println(test1.get(i));
- }
- //Using another For Loop to add more values to the ArrayList
- for (int i = 0 ; i < 10 ; i++){
- test1.add(2);
- }
- //Using the .get() Method to get the size of the ArrayList
- for (int i = 0 ; i < test1.size() ; i++){
- System.out.println(test1.get(i));
- }
- //Using a ForEach loop (A shortcut for writing a For Loop)
- System.out.println("Before the For Each Loop");
- for (Integer i : test1){
- System.out.println(i);
- }
- //Creating a new HashMap
- System.out.println();
- System.out.println("Testing HashMaps");
- HashMap<String, Integer> studentGrades = new HashMap<String, Integer>();
- studentGrades.put("Mark",87);
- studentGrades.put("Cassandra",95);
- studentGrades.put("Zenas",64);
- System.out.println(studentGrades.get("Mark"));
- //using .size() to get the number of elements in the HashMap
- System.out.println();
- System.out.println("This is the current size of the HashMap: " + studentGrades.size());
- //Accessing the keys as data
- System.out.println();
- System.out.println("I am outputting all of the current keys in the HashMap: " + studentGrades.keySet());
- //Using the ForEach Loop for a HashMap
- System.out.println();
- for (String i:studentGrades.keySet()){
- System.out.println(i + " has a grade of: " + studentGrades.get(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement