• Understand arrays and ArrayLists are a sequential collections of data.
  • Understand each element of an arrays and ArrayList is the same type.
  • Understand elements of an arrays and ArrayList have indices from 0 to their sizes-1
  • Understand how to declare arrays and ArrayList variables.
  • Understand how to allocate arrays and ArrayList objects.
  • Understand the relationship between arrays and ArrayList variables, objects, and memory.
  • Understand similarities and differences between arrays and ArrayLists.
  • Arrays, ArrayLists, and the Wirth Pattern

    We are solving problems with programs. The Wirth pattern from Primitive Types defines a prgram to be algorithms and data structures. Arrays and ArrayLists are part of the Data Structures component.

    Arrays and ArrayLists

    Arrays and ArrayLists are ordered, sequential collections of information, where each element must be the same type. Ordered and sequential in that each element has a position – zero-ith element, 1st element, 2nd element, and so on. For example, an array or ArrayList must contain all int, double, String, Person, etc.

    The following figure demonstrates the sequentialness of arrays and ArrayLists. Each element has an index with the first index 0 and the last index to N-1, where the total number of elements in the collection is N.

    Array/ArrayList

    Arrays and ArrayLists are reference types that reference objects. ArrayLists are more flexible than arrays because the total number of elements can grow and shrink.

    Sizes of String, arrays, ArrayList

    Strings, arrays, and ArrayLists are sequential collections of information. A String contains a sequence of characters. An array and an ArrayList contain a sequence of the same type. All three have a specific number of elements; however, the mechanism for getting the number of elements is different for each. You understand Strings. After studying this module, you will understand arrays and ArrayLists.

    • String - number characters retrieved via the instance method length.
      String s = "This";
      int l = s.length();  // l is 4
      
    • Arrays - number of elements retrieved via the field ```length``
      int[] ia = {1, 2, 3, 4};
      iny l = ia.length;  // l is 4
      
    • ArrayList - number of elements retrieved via the instance method size.
       ArrayList<Integer> ints = new ArrayList<Integer>();
       for (int i = 1; i < 5; i++)
          ints.add(i);
       int s = ints.size(); // s is 4
      

    Arrays and ArrayLists Lab

    The overview lab for arrays and arraylists is Arrays and ArrayLists.

    Tags: collection