4.10 Implementing ArrayList Algorithms

7 阅读1分钟

1. Exam Points

  • Implement ArrayList Algorithms using selection and loop statements
    • determine a minimum or maximum value
    • compute a sum or average
    • determine if at least one element has a particular property
    • determine if all elements have a particular property
    • determine the number of elements having a particular property
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements
    • shift or rotate elements left or right
    • reverse the order of the elements
    • insert elements
    • delete elements

2. Knowledge Points

(1) Implement ArrayList Algorithms

  • There are standard ArrayList algorithms that utilize traversals to:
    • determine a minimum or maximum value
    • compute a sum or average
    • determine if at least one element has a particular property
    • determine if all elements have a particular property
    • determine the number of elements having a particular property
    • access all consecutive pairs of elements
    • determine the presence or absence of duplicate elements
  • Example 1: compute a sum or average
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(10);
    list.add(20);
    list.add(30);
    list.add(40);
    
    double sum = 0.0;
    
    // 1. use a for loop
    // i: index
    // get(i): element
    for (int i = 0; i < list.size(); i++) {
        sum += list.get(i);
    }
    
    System.out.println("sum:" + sum);
    
    // 2. use a for-each loop: 
    // x : element
    sum = 0;
    for (Integer x : list) {
        sum += x;
    }
    
    System.out.println("sum:" + sum);
    
    // 3. get the average
    System.out.println(sum / list.size());
    
  • Example 2: Find a minimum or maximum value
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(11);
    list.add(41);
    list.add(12);
    list.add(30);
    
    // 1. use a for loop
    
    int max = list.get(0); // first element or Integer.MIN_VALUE
    
    for (int i = 0; i < list.size(); i++) {
        int x = list.get(i);
        if (x > max) {
            max = x;
        }
    }
    
    System.out.println("max:" + max);
    
    // 2. use a for-each loop
    max = list.get(0); // first element or Integer.MIN_VALUE
    for (int x : list) {
        if (x > max) {
            max = x;
        }
    }
    
    System.out.println("max:" + max);
    
  • Example 3: Check if a list is in increasing order
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(4);
    list.add(3);
    
    // Check if a list is increasing
    
    boolean isIncreasing = true;
    
    int prev = list.get(0);
    for (int i = 1; i < list.size(); i++) {
    
        Integer current = list.get(i);
        
        if (current < prev) {
            isIncreasing = false;
        }
        
        prev = current;
    }
    
    System.out.println(isIncreasing);
    
  • Example 4: count consecutive pairs
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(2);
    list.add(3);
    list.add(3);
    list.add(3);
    list.add(4);
    list.add(3);
    
    // count consecutive pairs
    
    int count = 0;
    
    for (int i = 0; i < list.size() - 1; i++) {
        if (list.get(i) == list.get(i + 1)) {
            count++;
        }
    }
    
    System.out.println(count);
    

3. Exercises