From 3908e0c10271a07c75af441bc4431d9e990682ca Mon Sep 17 00:00:00 2001 From: Peter Seibel Date: Sun, 31 Dec 2023 18:08:18 -0800 Subject: [PATCH 1/8] Edits to 7.1 --- .../topic-7-1-arraylist-basics.rst | 188 ++++++++++++++---- 1 file changed, 148 insertions(+), 40 deletions(-) diff --git a/_sources/Unit7-ArrayList/topic-7-1-arraylist-basics.rst b/_sources/Unit7-ArrayList/topic-7-1-arraylist-basics.rst index 071ce416..e7ed9c4d 100644 --- a/_sources/Unit7-ArrayList/topic-7-1-arraylist-basics.rst +++ b/_sources/Unit7-ArrayList/topic-7-1-arraylist-basics.rst @@ -21,12 +21,35 @@ Intro to ArrayLists Figure 1: A couple of lists -In the last unit, we learned about arrays to hold collections of related data. But arrays have limitations. The size of an array is established at the time of creation and cannot be changed. What if you don't know how big the collection of data will be? What if you want to add and remove items from the collection and change the size of the collection while the program is running? For example, if you wanted to represent a shopping list, you might add to the list throughout the week and remove things from the list while you are shopping. You probably would not know how many items will be on the list at the beginning of the week. +In the last unit, we learned about using arrays to hold collections of related +data. However arrays are not very flexible. Most notably, the size of an array +is established at the time of creation and cannot be changed. What if you don't +know how big the collection of data will be? What if you want to both add and +remove items from a collection? For example, if you wanted to represent a +shopping list, you might add to the list throughout the week and remove things +from the list while you are shopping. You probably would not know how many items +will be on the list at the beginning of the week. + +For cases like this, Java has a class called ``ArrayList`` which is a re-sizable +list. It is called ``ArrayList`` because it stores the items that have been +added to it in an underlying array. But it also takes care of keeping track of +how many items have been added to the array and it will create a new bigger +array under the covers when needed to hold more items. + +You can use ``ArrayList`` instead of arrays whenever you don't know the size of +the array you need or you know that you will add and remove items and may need +to change the array’s size dynamically during run time. An ``ArrayList`` is +**mutable**, meaning it can change during run time by adding and removing objects +from it. +.. note:: -Luckily, Java has a class called **ArrayList** which is a re-sizable array. An ArrayList has an underlying array that grows or shrinks as needed. You can use ArrayList instead of arrays whenever you don't know the size of the array you need or you know that you will add and remove items and may need to change the array's size dynamically during run time. An ArrayList is **mutable**, meaning it can change during runtime by adding and removing objects from it. - -An ArrayList is often called just a **list** on the CSA exam. In past AP CSA exams, the interface **List** is often used to declare an ArrayList. Interfaces are no longer on the exam, but if you see List being used, just assume it's an ArrayList. + An ``ArrayList`` is often called just a list on the CSA exam. Prior to 2020 + the AP CSA curriculum included **interfaces** which are somewhat like classes + and the interface ``List`` was often used to declare a variable that would + refer to an ``ArrayList``. Interfaces are no longer on the exam, but if you + see ``List`` being used in an old exam question just assume it’s an + ``ArrayList``. .. mchoice:: qloopList :answer_a: A list will always use less memory than an array. @@ -43,32 +66,55 @@ An ArrayList is often called just a **list** on the CSA exam. In past AP CSA exa -Import Package +Packages and imports ------------------------ .. index:: single: import statement -The ``ArrayList`` class is in the ``java.util`` package. A **package** is a set or library of related classes. The java.lang package is the main Java language classes that you get automatically without importing it. The java.util package has a lot of utility classes that you can use if you import the package. If you want to use any class other than those in ``java.lang`` you will need to either use the full name (packageName.ClassName) like (``java.util.ArrayList``) or use one or more import statements to import in that package. - -Import statements have to be the first code in a Java source file. An import statement tells Java which class you mean when you use a short name (like ``ArrayList``). It tells Java where to find the definition of that class. - -You can import just the classes you need from a package as shown below. Just provide an ``import`` statement for each class that you want to use. +The ``ArrayList`` class is in the ``java.util`` package. A **package** is a set +or library of related classes. The classes we have used until now, such as +``String`` and ``Math``, are in the special package ``java.lang`` whose classes +are always available in any Java program. Other packages, such as ``java.util``, +provide classes that can only be used either by **importing** them or (much more +rarely) by referring to them by their full name which includes the package as a +prefix. The full name of ``ArrayList`` is thus ``java.util.ArrayList`` but +rather than type that out all the time, in any class where we want to use +``ArrayList`` we will usually import it with an ``import`` statement. + +Import statements have to come before the class definition in a Java source file +and serve to tell Java which class you mean when you use a short name like +``ArrayList``. To import just one class we use a single ``import`` of the +fully-qualified name of the class like this: .. code-block:: java - // import just the ArrayList class + // Import just the ArrayList class from java.util + import java.util.ArrayList; .. index:: single: package pair: statement; import -Another option is to import everything at the same level in a package using ``import packageName.*``. +After such an import statement, anywhere ``ArrayList`` is used as a class name +in the file it will be taken to mean ``java.util.ArrayList``. + +Another option is to import all the classes in a package with a “wildcard” import: .. code-block:: java - import java.util.*; // import everything in package including ArrayList + // Import everything in java.util including ArrayList + import java.util.*; + +This import statement will also cause, ``ArrayList`` to refer +``java.util.ArrayList``. But many other names of classes defined in the +``java.util`` package will also be available whether you use them or not. (One +that you have probably used by now is ``Scanner`` which can be used to read +input a user types at the command line.) Using wildcard imports can cause +conflicts if you import all the classes from two different packages and they +have class names in common but usually that’s not a problem, at least with +packages that are part of Java itself. .. note:: @@ -92,9 +138,15 @@ Another option is to import everything at the same level in a package using ``im Declaring and Creating ArrayLists ---------------------------------- -To declare a ArrayList use ``ArrayList name`` Change the *Type* to be whatever type of objects you want to store in the ArrayList, for example ``String`` as shown in the code below. You don't have to specify the **generic type** ````, since it will default to ``Object``, but it is good practice to specify it to restrict what to allow in your ArrayList. Using a type ArrayList is preferred over just using ArrayList because it allows the compiler to find errors that would otherwise be missed until run-time. - - +To declare a ArrayList use ``ArrayList name`` where *Type*, called a +**type parameter** is the type of objects you want to store in the ArrayList. +For example a variable naming an ``ArrayList`` meant to hold ``String``\ s is +declared as ``ArrayList`` as shown in the code below. You can declare a +variable to just be of type ``ArrayList``, with no type parameter, and it’ll be +approximately the same as if you had declared ``ArrayList``, but it is +good practice to specify the type of objects you intend to store in an +``ArrayList`` as it allows the compiler to find errors that would otherwise be +missed until run time. .. code-block:: java @@ -104,7 +156,13 @@ To declare a ArrayList use ``ArrayList name`` Change the *Type* to be wha .. note:: - ArrayLists can only hold objects like String and the wrapper classes Integer and Double. They cannot hold primitive types like int, double, etc. + ``ArrayList``\ s can only hold reference types like ``String``. Since they + can’t hold primitive types like ``int`` and ``double``, if we want a + collection of numbers we need to use the wrapper classes ``Integer`` or + ``Double``. However, because of autoboxing, if you declare an + ``ArrayList`` or ``ArrayList`` you can mostly treat the + elements of the ``ArrayList`` as if they were in fact ``int``\ s or + ``double``\ s. |CodingEx| **Coding Exercise** @@ -113,7 +171,10 @@ To declare a ArrayList use ``ArrayList name`` Change the *Type* to be wha :language: java :autograde: unittest - In the code below we are declaring a variable called ``nameList`` that can refer to a ArrayList of strings, but currently doesn't refer to any ArrayList yet (it's set to ``null``). + In the code below we are declaring a variable called ``nameList`` that can + refer to a ``ArrayList`` of strings, but currently doesn't refer to any + ``ArrayList`` yet as it’s set to ``null``. + ~~~~ import java.util.*; // import for ArrayList @@ -151,16 +212,22 @@ To declare a ArrayList use ``ArrayList name`` Change the *Type* to be wha } } -Declaring a ArrayList doesn't actually create a ArrayList. It only creates a variable that can refer to a ArrayList. To actually create a ArrayList use ``new ArrayList()``. If you leave off the ```` it will default to ``Object``. +As with other reference types, declaring a ``ArrayList`` variable doesn't +actually create a ``ArrayList`` object. It only creates a variable that can +refer to a ``ArrayList`` or ``null``. To actually create a ``ArrayList`` we must +invoke a constructor such as ``new ArrayList()``. -You can get the number of items in a ArrayList using the ``size()`` method. Notice that an empty ArrayList has a size of 0 because the ArrayList constructor constructs an empty list. Also notice that you can't get the size of a ArrayList that is currently set to ``null`` on line 9. You will get a ``NullPointerException`` instead, which means that you tried to do something with an object reference that was ``null`` (doesn't exist). +You can get the number of items in a ``ArrayList`` using the ``size()`` method. +Notice that a newly constructed ``ArrayList`` is empty and thus has a size of 0. +Also remember that you can’t call methods on ``null`` so trying to call ``size`` +on the value of ``list2`` at line 10 below causes a ``NullPointerException``. .. activecode:: ArrayListCreateStr :language: java :autograde: unittest :practice: T - The following code demonstrates a NullPointerException. Change the list2 declaration so that it creates a new Arraylist to remove the NullPointerException. + The following code demonstrates a NullPointerException. Change the list2 declaration so that it creates a new ArrayList to remove the NullPointerException. ~~~~ import java.util.*; // import needed for ArrayList @@ -200,8 +267,17 @@ You can get the number of items in a ArrayList using the ``size()`` method. Not } } -You can also create ArrayLists of integer values. However, you have to use ``Integer`` as the type because ArrayLists can only hold objects, not primitive values. All primitive types must be **wrapped** in objects before they are added to an ArrayList. For example, ``int`` values can be wrapped in ``Integer`` objects, ``double`` values can be wrapped in ``Double`` objects. You can actually put in any kind of Objects in an ArrayList, even for a class that you wrote in Unit 5 like Student or Person or Pet. +You can also create ArrayLists of integer and double values. However, you have +to use ``Integer`` or ``Double`` as the type parameter because ``ArrayList``\ s +can only hold objects, not primitive values. All primitive types must be +**wrapped** in objects before they are added to an ArrayList. For example, +``int`` values can be wrapped in ``Integer`` objects, ``double`` values can be +wrapped in ``Double`` objects. However this normally happens automatically +thanks to autoboxing. +You can actually put in any kind of objects in an ``ArrayList``, including +instances of classes that you write, such as the ``Student``, ``Person``, or +``Pet`` classes from Unit 5. .. activecode:: ArrayListCreateInt :language: java @@ -209,7 +285,7 @@ You can also create ArrayLists of integer values. However, you have to use ``In Here's an example of a Integer ArrayList. ~~~~ - import java.util.*; // import everything at this level + import java.util.*; public class ArrayListCreateInt { @@ -264,7 +340,7 @@ You can also create ArrayLists of integer values. However, you have to use ``In Although it is not on the AP exam, you can convert an array to a ``List`` using the static method ``asList`` from the ``Arrays`` helper class: -``Arrays.asList(arrayname)``. Note that ``ArrayList` has a ``toString`` method +``Arrays.asList(arrayname)``. Note that ``ArrayList`` has a ``toString`` method that is automatically called to print the list in a nice format. .. activecode:: ArrayListFromArray @@ -312,7 +388,13 @@ that is automatically called to print the list in a nice format. |CodingEx| **Coding Exercise** -You can add values to an ArrayList by using its **add** method, described in detail in the next lesson. Try the code below. Note that the type of the ArrayList, String or Integer, also determines the type of parameters and return types for all of its methods, so add and print work for any type of ArrayList. +You can add values to an ``ArrayList`` using its **add** method, described in +detail in the next lesson. Try the code below. Note that the type of the +``ArrayList``, ``String`` or ``Integer``, also determines the type of parameters +and return types for all of its methods, so add and print work for any type of +``ArrayList``. And when the ``ArrayList`` is a list of ``Integer``\ s, +autoboxing takes care of wrapping the ``int`` arguments like ``2`` and ``4`` +into instances of ``Integer`` for us. .. activecode:: listAdd :language: java @@ -383,9 +465,18 @@ You can add values to an ArrayList by using its **add** method, described in det 2017 Free Response Question -This programming challenge is based on the |FRQ 2017| part 1a on the 2017 AP CSA exam. In this question, you are asked to write a constructor for a class called Digits. This constructor takes an integer number as its argument and divides it up into its digits and puts the digits into an ArrayList. For example, new Digits(154) creates an ArrayList with the digits [1, 5, 4]. +This programming challenge is based on the |FRQ 2017| part 1a on the 2017 AP CSA +exam. In this question, you are asked to write a constructor for a class called +``Digits``. This constructor takes an integer number as its argument and divides +it up into its digits and puts the digits into an ``ArrayList``. For example, +``new Digits(154)`` creates an ArrayList with the digits [1, 5, 4]. -First, let's discuss how to break up a number into its digits. Try the code below. What happens if you divide an integer by 10? Remember that in integer division the result truncates (cuts off) everything to the right of the decimal point. Which digit can you get by using ``% 10`` which returns the remainder after dividing by 10? Try a different number and guess what it will print and then run to check. +First, let’s discuss how to break up a number into its digits. Try the code +below. What happens if you divide an integer by 10? Remember that in integer +division the result truncates (cuts off) everything to the right of the decimal +point. Which digit can you get by using ``% 10`` which returns the remainder +after dividing by 10? Try a different number and guess what it will print and +then run to check. .. activecode:: divideby10 :language: java @@ -430,14 +521,21 @@ First, let's discuss how to break up a number into its digits. Try the code belo } } -We can use a while loop to print out each digit in reverse order starting from the right (4, 5, 1 for the number 154) while dividing it by 10. You can try it in the active code above. Here is the pseudocode: +We can use a while loop to print out each digit in reverse order starting from +the right (4, 5, 1 for the number 154) while dividing it by 10. You can try it +in the active code above. Here is the pseudocode: - - while number is greater than 0 +- while number is greater than 0 - - print out the last digit using % - - change the number to cut off the last digit using / + - print out the last digit using % + - change the number to cut off the last digit using / -Now, let's write a constructor for the Digits class that uses this loop and adds each found digit to the ArrayList instead of printing it out. You can use a special method called **Collections.reverse(digitsList);** to reverse the order of the digits in the ArrayList after the loop to get them in the right order. In the next lesson, we will also learn how to use a different add method that adds in elements at any index instead of the end. +Now, let’s write a constructor for the ``Digits`` class that uses this loop and +adds each found digit to the ``ArrayList`` instead of printing it out. You can +use a special method called ``Collections.reverse(digitsList);`` to reverse the +order of the digits in the ``ArrayList`` after the loop to get them in the right +order. In the next lesson, we will also learn how to use a different ``add`` +method that adds in elements at any index instead of the end. .. activecode:: challenge-7-1-digits :language: java @@ -515,18 +613,28 @@ Now, let's write a constructor for the Digits class that uses this loop and adds Summary ----------- -- ArrayList are re-sizable arrays that allow adding and removing items to change their size during run time. +- ``ArrayList``\ s are re-sizable lists that allow adding and removing items to + change their size during run time. -- The ArrayList class is in the java.util package. You must import java.util.* to use it. +- The ``ArrayList`` class is in the ``java.util`` package. You must import + ``java.util.ArrayList`` or ``java.util.*`` to use it. -- An ArrayList object contains object references and is mutable, meaning it can change (by adding and removing items from it). +- An ``ArrayList`` object contains object references and is mutable, meaning it + can change (by adding and removing items from it). -- The ArrayList constructor ArrayList() constructs an empty list of size 0. +- The ``ArrayList`` constructor ``ArrayList()`` constructs an empty list of size 0. -- Java allows the generic type ArrayList, where the generic type E specifies the type of the elements, like String or Integer. Without it, the type will be Object. +- Java allows the generic type ``ArrayList``, where the generic type ``E`` + specifies the type of the elements, like ``String`` or ``Integer``. Without + it, the type will be ``Object``. -- ArrayList is preferred over ArrayList because it allows the compiler to find errors that would otherwise be found at run-time. +- ``ArrayList`` is preferred over ``ArrayList`` because it allows the + compiler to find errors that would otherwise be found at run time. -- When ArrayList is specified, the types of the reference parameters and return type when using its methods are type E. +- When ``ArrayList`` is specified, the types of the reference parameters and + return type when using its methods are type ``E``. -- ArrayLists cannot hold primitive types like int or double, so you must use the wrapper classes Integer or Double to put numerical values into an ArrayList. +- ``ArrayList``\ s cannot hold primitive types like ``int`` or ``double``, so + you must use the wrapper classes ``Integer`` or ``Double`` to put numerical + values into an ``ArrayList``. However autoboxing usually takes care of that + for you. From 48486ea3e274d71db149f39a2862e77f2a7924a4 Mon Sep 17 00:00:00 2001 From: Peter Seibel Date: Sun, 31 Dec 2023 19:32:45 -0800 Subject: [PATCH 2/8] Edits to 7.2 --- .../topic-7-2-arraylist-methods.rst | 164 +++++++++++------- 1 file changed, 104 insertions(+), 60 deletions(-) diff --git a/_sources/Unit7-ArrayList/topic-7-2-arraylist-methods.rst b/_sources/Unit7-ArrayList/topic-7-2-arraylist-methods.rst index 2e94e736..f13f5a67 100644 --- a/_sources/Unit7-ArrayList/topic-7-2-arraylist-methods.rst +++ b/_sources/Unit7-ArrayList/topic-7-2-arraylist-methods.rst @@ -24,31 +24,33 @@ ArrayList Methods AP CSA Java Quick Reference Sheet -The following are the ``ArrayList`` methods that you need to know for the AP CSA exam. These are included on the |AP CSA Reference Sheet| that you will receive during the exam so you do not need to memorize them. The E in the method headers below stands for the type of the element in the ArrayList; this type E can be any Object type. We will look at how these methods work below. +The following are the ``ArrayList`` methods that you need to know for the AP CSA +exam. These are included on the |AP CSA Reference Sheet| that you will receive +during the exam so you do not need to memorize them. The E in the method headers +below stands for the type of the element in the ArrayList; this type E can be +any Object type. We will look at how these methods work below. - - **int size()** returns the number of elements in the list +- **int size()** returns the number of elements in the list - - **boolean add(E obj)** appends obj to the end of the list and returns true +- **boolean add(E obj)** appends obj to the end of the list and returns true - - **E remove(int index)** removes the item at the index and shifts remaining items to the left (to a lower index) +- **E remove(int index)** removes the item at the index and shifts remaining items to the left (to a lower index) - - **void add(int index, E obj)** moves any current objects at index or beyond to the right (to a higher index) and inserts obj at the index +- **void add(int index, E obj)** moves any current objects at index or beyond to the right (to a higher index) and inserts obj at the index - - **E get(int index)** returns the item in the list at the index +- **E get(int index)** returns the item in the list at the index - - **E set(int index, E obj)** replaces the item at index with obj +- **E set(int index, E obj)** replaces the item at index with obj - - - - -Size() -------- +``size()`` +---------- .. index:: pair: arraylist; size -As we saw in the last lesson, you can get the number of items in a ArrayList using its ``size()`` method. The ArrayList starts out empty with a size of 0. +As we saw in the last lesson, you can get the number of items in a ``ArrayList`` +using its ``size()`` method. The ``ArrayList`` starts out empty with a size +of 0. .. code-block:: java @@ -57,26 +59,33 @@ As we saw in the last lesson, you can get the number of items in a ArrayList usi .. note:: - With arrays, you use the ``length`` field to get the number of items in the array. But, with an ``ArrayList`` you use the ``size()`` method to get the number of items in the ArrayList. You will not be penalized if you mix up length and size() in the CSA exam. The number of items in an empty ArrayList is 0. + With arrays, you use the ``length`` field to get the number of items in the + array. But, with an ``ArrayList`` you use the ``size()`` method to get the + number of items in the ``ArrayList``. You will not be penalized if you mix up + ``length`` and ``size()`` in the CSA exam. The number of items in an empty + ``ArrayList`` is 0. -Add(obj) to an ArrayList ------------------------------ +``add(obj)`` +------------ .. index:: pair: arraylist; add -You can add values to an ArrayList by using the method ``add(obj)`` which will add the object to the end of the list, just like you would join the end of the line to board a bus. +You can add values to an ``ArrayList`` using the method ``add(obj)`` which will +add the object to the end of the list, just like you would join the end of the +line to board a bus. |CodingEx| **Coding Exercise** - - - .. activecode:: listAdd1 :language: java :autograde: unittest - Run the code below to see how the list changes as each object is added to the end. Notice that we added the same string to the list more than once. Lists can hold duplicate objects. Can you add your name to the list and then print out the list? + Run the code below to see how the list changes as each object is added to the + end. Notice that we added the same string to the list more than once. Lists + can hold duplicate objects. Can you add your name to the list and then print + out the list? + ~~~~ import java.util.*; // import all classes in this package. @@ -137,7 +146,13 @@ You can add values to an ArrayList by using the method ``add(obj)`` which will a pair: list; autoboxing pair: list; unboxing -When adding Integer objects to the list, you can use the Integer constructor like ``add(new Integer(5))`` in Java version 7 which is used on the exam (although this is deprecated and no longer used in Java version 9) or you can just add the int value directly like ``add(5)`` in any Java version and it will be changed into an ``Integer`` object automatically. This is called **autoboxing**. When you pull an ``int`` value out of a list of ``Integers`` that is called **unboxing**. +When adding Integer objects to the list, you can use the Integer constructor +like ``add(new Integer(5))`` in Java version 7 which is used on the exam +(although this is deprecated and no longer used in Java version 9) or you can +just add the int value directly like ``add(5)`` in any Java version and it will +be changed into an ``Integer`` object automatically. This is called +**autoboxing**. When you pull an ``int`` value out of a list of ``Integers`` +that is called **unboxing**. .. code-block:: java @@ -145,13 +160,16 @@ When adding Integer objects to the list, you can use the Integer constructor lik list.add(new Integer(5)); // this will only work in Java 7 list.add(5); // this will work in all Java versions -You can put any kind of Objects into an ArrayList. Even objects for a class that you wrote. For example, here is an ArrayList of Students. +You can put any kind of objects into an ``ArrayList``. Even instances of a class +that you wrote. For example, here is an ``ArrayList`` of ``Student``\ s. .. activecode:: StudentArrayList :language: java :autograde: unittest - An example of an ArrayList of Student objects. Add a new student with your name and info in it. + An example of an ``ArrayList`` of ``Student`` objects. Add a new student with + your name and info in it. + ~~~~ import java.util.*; @@ -213,21 +231,25 @@ You can put any kind of Objects into an ArrayList. Even objects for a class that } } -Add(index,obj) in an ArrayList ------------------------------- - -There are actually two different ``add`` methods in the ``ArrayList`` class. The ``add(obj)`` method adds the passed object to the end of the list. The ``add(index,obj)`` method adds the passed object at the passed index, but first moves over any existing values to higher indicies to make room for the new object. +``add(index,obj)`` +------------------ +There are actually two different ``add`` methods in the ``ArrayList`` class. The +``add(obj)`` method adds the passed object to the end of the list. The +``add(index,obj)`` method adds the passed object at the passed index, but first +moves over any existing values to higher indices to make room for the new +object. |CodingEx| **Coding Exercise** - - .. activecode:: listAddInt2 :language: java :autograde: unittest - What will the code below print out? Try figuring it out before running it. Remember that ArrayLists start at index 0 and that the add(index,obj) always has the index as the first argument. + What will the code below print out? Try figuring it out before running it. + Remember that ``ArrayList``\ s start at index 0 and that the add(index,obj) always + has the index as the first argument. + ~~~~ import java.util.*; // import all classes in this package. @@ -278,7 +300,7 @@ There are actually two different ``add`` methods in the ``ArrayList`` class. Th .. note:: - ArrayLists like arrays start numbering their elements from 0. + ``ArrayList``\ s like arrays start numbering their elements from 0. |Exercise| **Check your understanding** @@ -340,13 +362,16 @@ You can step through the code above by clicking on this |Java Visualizer 1|. You can step through the code above by clicking on the following |Java visualizer 2|. -Remove(index) from ArrayList ----------------------------------- +``remove(index)`` +----------------- .. index:: pair: arraylist; removing an item -You can also remove values from an ArrayList by using **remove(index)** to remove the item at the given index from the list. This will move all the other items over in the underlying array and decrease the size of the ArrayList by 1. +You can also remove values from an ``ArrayList`` using the ``remove(index)`` +method. It removes and returns the item at the given index. This will move all +the other items over in the underlying array and decrease the size of the +``ArrayList`` by 1. |CodingEx| **Coding Exercise** @@ -401,7 +426,10 @@ You can also remove values from an ArrayList by using **remove(index)** to remov .. note:: - The ``remove(int index)`` method will remove the object at the index and shift left any values to the right of the current index. It doesn't remove the object that matches the integer value given. In the example above it doesn't remove the value 1. It removes the value 2 at index 1. + The ``remove(int index)`` method will remove the object at the given index + and shift left any values to the right of that index. It doesn't remove the + object that matches the integer value given. In the example above it doesn't + remove the value 1. It removes the value 2 at index 1. .. mchoice:: qListRem :answer_a: [2, 3] @@ -428,21 +456,25 @@ You can also remove values from an ArrayList by using **remove(index)** to remov You can step through the code above by clicking on the following `RemoveExample `_. -ArrayList get/set Methods ------------------------------------- +``get(index)`` and ``set(index, obj)`` +-------------------------------------- .. index:: pair: arraylist; getting an item pair: arraylist; setting an item -You can get the object at an index using ``obj = listName.get(index)`` and set the object at an index using ``listName.set(index,obj)``. Set/Get are used after you add and remove elements to an ArrayList to change or retrieve them. +You can get the object at an index using ``obj = listName.get(index)`` and set +the object at an index using ``listName.set(index,obj)``. Both methods require +that the index argument refer to an existing element of the list, i.e. the index +must be greater than or equal to 0 and less than the ``size()`` of the list. -Notice that ArrayLists use set/get methods instead of using the square brackets array[index] that arrays use. This is because ArrayList is a class with methods that provide access to the underlying array. +Notice that ``ArrayList``\ s use ``get`` and ``set`` methods instead of the +index operator that we use with arrays: ``array[index]``. This is because +``ArrayList`` is a class with methods, not a built in type with special support +in the language like arrays. |CodingEx| **Coding Exercise** - - .. activecode:: listGetSet :language: java :autograde: unittest @@ -561,18 +593,24 @@ You can step through the code above by clicking on the following `Example2 highScoreList = new ArrayList(); @@ -582,11 +620,10 @@ Here is a comparison of how to create arrays and ArrayLists: comparison handout -Here is a comparison of how to access and change elements in arrays and ArrayLists. -Note that ArrayLists have a method ``size()`` instead of a ``length`` property, and -ArrayLists use get/set methods instead of square brackets ([]). -Here is a |comparison handout| of the basic operations to access 1-dimensional and 2-dimensional arrays -(which we will see in the next unit), ArrayLists, and Strings made by AP CSA teacher Sam Procopio of Bishop Blanchet High School. +Here is a comparison of how to access and change elements in arrays and +``ArrayList``\ s. Note that ``ArrayList``\ s have a method ``size()`` instead of a +``length`` property, and ``ArrayList``\ s use ``get``/``set`` methods instead of +the index operator (``[]``). =========== ======================== ======================== Operation array ArrayList @@ -596,21 +633,28 @@ length/size array.length list.size() Access value = array[index]; value = list.get(index); ----------- ------------------------ ------------------------ Modify array[index] = value; list.set(index,value); ------------ ------------------------ ------------------------ =========== ======================== ======================== -Note that the ArrayList methods add and remove do not have a simple equivalent in arrays because they actually change the size of the underlying array and move elements over. +Note that the ``ArrayList`` methods ``add`` and ``remove`` do not have a simple +equivalent in arrays because they change the number of elements in the list and +may shift the positions of other elements. + +Here is a |comparison handout| of the basic operations to access 1-dimensional +and 2-dimensional arrays (which we will see in the next unit), ``ArrayList``\ s, +and ``String``\ s made by AP CSA teacher Sam Procopio of Bishop Blanchet High +School. |Groupwork| Programming Challenge : Array to ArrayList ------------------------------------------------------- - - .. activecode:: challenge-7-2-array-to-arraylist :language: java :autograde: unittest - Rewrite the following code that uses an array to use an ArrayList instead. In the comments write why you think an ArrayList is a better data structure to use than an array for this problem. + Rewrite the following code that uses an array to use an ``ArrayList`` + instead. In the comments write why you think an ``ArrayList`` is a better + data structure to use than an array for this problem. + ~~~~ import java.util.*; @@ -700,7 +744,7 @@ Summary - **int size()** : Returns the number of elements in the list - **boolean add(E obj)** : Appends obj to end of list; returns true - - **void add(int index, E obj)** : Insertss obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size + - **void add(int index, E obj)** : Inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size - **remove(int index)** — Removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index - **E get(int index)** : Returns the element at position index in the list - **E set(int index, E obj)** : Replaces the element at position index with obj; returns the element formerly at position index From 6d3d92ee15e1976201c6441f055345c2f4fe77b1 Mon Sep 17 00:00:00 2001 From: Peter Seibel Date: Sun, 31 Dec 2023 20:51:26 -0800 Subject: [PATCH 3/8] Edits to 7.3 --- .../topic-7-3-arraylist-loops.rst | 151 ++++++++++++------ 1 file changed, 105 insertions(+), 46 deletions(-) diff --git a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst index 16bad285..fdf23472 100644 --- a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst +++ b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst @@ -6,10 +6,12 @@ |Time90| -Traversing ArrayLists with Loops +Traversing ``ArrayList``\ s with Loops ================================ -While loops, for loops, and enhanced for each loops can all be used to traverse an ArrayList just like an array. +``ArrayList``\ s can be traversed with ``while`` loops and both regular and +enhanced ``for`` loops much the same way we use those constructs to loop over an +array. Enhanced For Each Loop ---------------------- @@ -17,11 +19,19 @@ Enhanced For Each Loop .. index:: pair: list; for-each loop -You can use a enhanced for-each loop to traverse through all of the items in a list, just like you do with an array as shown in the main method below. - -|CodingEx| **Coding Exercise** +You can use a enhanced ``for`` loop to traverse all of the items in an +``ArrayList``, just like you do with an array when you only care about the +values in the list and not their indices. An example is shown in the ``main`` +method below. +Note however that you can’t use the enhanced ``for`` loop if you want to add or +remove elements while traversing an ``ArrayList``. If an ``ArrayList`` is +modified, such as by calling the ``add`` or ``remove`` methods, while it is +being looped over, it will cause the loop to throw a +``ConcurrentModificationException``. If you need to modify an ``ArrayList`` +while looping over it, you’ll need to use a regular ``while`` or ``for`` loop. +|CodingEx| **Coding Exercise** .. activecode:: listForEachLoop :language: java @@ -96,19 +106,28 @@ You can use a enhanced for-each loop to traverse through all of the items in a l For Loop ---------------------- -You can also use a ``while`` or ``for`` loop to process list elements using the index. The ArrayList index starts at 0 just like arrays, but instead of using the square brackets [] to access elements, you use the ``get(index)`` to get the value at the index and ``set(index,value)`` to set the element at an index to a new value. -If you try to use an index that is outside of the range of 0 to the number of elements − 1 in an ArrayList, your code will throw an **ArrayIndexOutOfBoundsException**, just like in arrays. - -|CodingEx| **Coding Exercise** +You can also use a ``while`` loop or a regular ``for`` loop to process list +elements accessed using an index. ``ArrayList`` indices starts at 0 just like +array indices, but instead of using the index operator ``[]`` to access +elements, you use the ``get(index)`` method to get the value at the index and +``set(index,value)`` to set the element at an index to a new value. +If you try to use an index that is outside of the range of 0 to the number of +elements − 1 in an ArrayList, your code will throw an +``IndexOutOfBoundsException``, similar to the ``ArrayIndexOutOfBoundsException`` +thrown if you use the index operator on an array with an index out of bounds for +that array. +|CodingEx| **Coding Exercise** .. activecode:: listForLoop :language: java :autograde: unittest :practice: T - The following code will throw an ArrayIndexOutOfBoundsException. Can you fix it? + The following code will throw an ``IndexOutOfBoundsException``. Can you fix + it? + ~~~~ import java.util.*; @@ -158,7 +177,9 @@ If you try to use an index that is outside of the range of 0 to the number of el While Loop ---------------------- -The example below demonstrates a while loop and an object-oriented approach where the list is a field of the current object and you use an object method rather than a class (static) method to loop through the list. +The example below demonstrates a ``while`` loop and an object-oriented approach +where the list is a field of the current object and you use an object method +rather than a class (static) method to loop through the list. |CodingEx| **Coding Exercise** @@ -192,7 +213,10 @@ The example below demonstrates a while loop and an object-oriented approach wher nameList.remove(index); found = // true or false? } - else index++; + else + { + index++; + } } return found; } @@ -230,9 +254,16 @@ The example below demonstrates a while loop and an object-oriented approach wher } } -Be careful when you remove items from a list as you loop through it. Remember that removing an item from a list will shift the remaining items to the left. Notice that the method above only increments the current index if an item was not removed from the list. If you increment the index in all cases you will miss checking some of the elements since the rest of the items shift left when you remove one. - -Do not use the enhanced for each loop if you want to add or remove elements when traversing a list because it will throw a **ConcurrentModificationException** error. Since for each loops do not use an index, you cannot do this special case of incrementing only if it is changed. So if you are going to add or remove items or you need the index, use a regular for-loop or a while loop. +Be careful when you remove items from a list as you loop through it. Notice how +the method above only increments the index if an item was not removed from the +list. This is because removing an item from a list will shift the remaining +items to the left and if you increment the index in all cases you will skip the +elements immediately after each element you remove. To see why, consider that +those elements will be shifted into the position of the just removed element and +if you increment the index, it will move to the next position, skipping the +element that used to be at that position. Leaving the index unchanged after a +remove allows the shifted-down element to be processed on the next time through +the loop. |Exercise| **Check your understanding** @@ -341,7 +372,9 @@ ArrayList of Student Objects |CodingEx| **Coding Exercise** -You can put any kind of Objects into an ArrayList. For example, here is an ArrayList of Students. Although the print statement works here, you may want a nicer printout. +You can put any kind of objects into an ``ArrayList``. For example, here is an +``ArrayList`` of ``Student``\ s. Although the print statement works here, you +may want a nicer printout. .. activecode:: StudentList :language: java @@ -372,11 +405,11 @@ You can put any kind of Objects into an ArrayList. For example, here is an Array private String email; private int id; - public Student(String initName, String initEmail, int initId) + public Student(String name, String email, int id) { - name = initName; - email = initEmail; - id = initId; + this.name = name; + this.email = email; + this.id = id; } // toString() method @@ -421,9 +454,10 @@ You can put any kind of Objects into an ArrayList. For example, here is an Array -This challenge is based on the |2018 Free Response Question #2 WordPair|. We encourage you to work in pairs on this challenge. +This challenge is based on the |2018 Free Response Question #2 WordPair|. We +encourage you to work in pairs on this challenge. -You are given a class called WordPair that can store pairs of words. +You are given a class called ``WordPair`` that can store pairs of words. .. code-block:: java @@ -432,10 +466,10 @@ You are given a class called WordPair that can store pairs of words. private String word1; private String word2; - public WordPair(String w1, String w2) + public WordPair(String word1, String word2) { - word1 = w1; - word2 = w2; + this.word1 = word1; + this.word2 = word2; } public String getFirst() @@ -454,7 +488,8 @@ You are given a class called WordPair that can store pairs of words. } } -First, see if you can create an ArrayList of WordPair Objects below. Look at the StudentList example above for help. +First, see if you can create an ``ArrayList`` of ``WordPair`` objects below. +Look at the ``StudentList`` example above for help. .. activecode:: ArrayListWordPair1 :language: java @@ -481,10 +516,10 @@ First, see if you can create an ArrayList of WordPair Objects below. Look at the private String word1; private String word2; - public WordPair(String w1, String w2) + public WordPair(String word1, String word2) { - word1 = w1; - word2 = w2; + this.word1 = word1; + this.word2 = word2; } public String getFirst() @@ -539,22 +574,33 @@ First, see if you can create an ArrayList of WordPair Objects below. Look at the :align: left :figclass: align-center -In this FRQ, you are given an array of words and you will create pairs of them by taking the first word and pairing it with all the other words, then taking the second word and pairing it with all but the first one, and so on. For example, if the word array is ["Hi", "there", "Tyler", "Sam"], this figure shows how the word pairs are formed. +In this FRQ, you are given an array of words and you will create pairs of them +by taking the first word and pairing it with all the other words, then taking +the second word and pairing it with all but the first one, and so on. For +example, if the word array is ["Hi", "there", "Tyler", "Sam"], this figure shows +how the word pairs are formed. + +In the class ``WordPairsList`` below, you will write the constructor which takes +the array of words and pairs them up as shown in the figure. You will need +nested loops to pair each element with the rest of the elements in the list. +Here is the pseudocode. -In the class WordPairsList below, you will write the constructor which takes the array of words and pairs them up as shown in the figure. You will need nested loops to pair each element with the rest of the elements in the list. Here is the pseudocode. +- Initialize the ``allPairs`` list to an empty ``ArrayList`` of ``WordPair`` objects. - - Initialize the allPairs list to an empty ArrayList of WordPair objects. - - Loop through the words array for the first word in the word pair (for loop from index i = 0 to length-1) +- Loop through the ``words`` array for the first word in the word pair (for loop from index ``i = 0`` to ``length-1``) - - Loop through the rest of the word array starting from index i+1 for the second word in the word pair (for loop from index j = i+1 to length) + - Loop through the rest of the word array starting from index ``i + 1`` for the second word in the word pair (for loop from index ``j = i + 1`` to ``length``) - - Add the new WordPair formed from the ith word and the jth word to the allPairs ArrayList. + - Add the new ``WordPair`` formed from the *i*th word and the *j*th word to the ``allPairs`` ``ArrayList``. .. activecode:: challenge-7-3-WordPairs :language: java :autograde: unittest - FRQ WordPairs Challenge: Complete the constructor for WordPairsList below which will add pairs of words from a given array to the ArrayList. Then, complete the method numMatches(). + FRQ WordPairs Challenge: Complete the constructor for ``WordPairsList`` below + which will add pairs of words from a given array to the ``ArrayList``. Then, + complete the method ``numMatches()`` as described below this exercise. + ~~~~ import java.util.*; @@ -598,10 +644,10 @@ In the class WordPairsList below, you will write the constructor which takes the private String word1; private String word2; - public WordPair(String w1, String w2) + public WordPair(String word1, String word2) { - word1 = w1; - word2 = w2; + this.word1 = word1; + this.word2 = word2; } public String getFirst() @@ -697,18 +743,31 @@ In the class WordPairsList below, you will write the constructor which takes the } } -In the next part of the FRQ challenge, you are asked to write a method called ``numMatches`` that counts and returns the number of pairs where the first word is the same as the second word. For example, if the word array is ``["hi","bye","hi"]``, the pairs generated would be ``["hi","bye"]``, ``["hi","hi"]``, and ``["bye","hi"]``. In the second pair ``["hi","hi"]``, the first word is the same as the second word, so ``numMatches`` would return 1. +In the next part of the FRQ challenge, you are asked to write a method called +``numMatches`` that counts and returns the number of pairs where the first word +is the same as the second word. For example, if the word array is +``["hi","bye","hi"]``, the pairs generated would be ``["hi","bye"]``, +``["hi","hi"]``, and ``["bye","hi"]``. In the second pair ``["hi","hi"]``, the +first word is the same as the second word, so ``numMatches`` would return 1. + +For this method, you will need a loop that goes through the ``ArrayList`` +``allPairs`` and for each ``WordPair`` in ``allPairs``, it checks to see if its +first word (using the ``getFirst`` method) equals the second word (using the +``getSecond`` method). If there is a match, it increments a counter which it +returns at the end of the method. To test this method, add another "there" into +the words array and then uncomment the call to ``numMatches``. -For this method, you will need a loop that goes through the ``ArrayList`` ``allPairs`` and for each ``WordPair`` in ``allPairs``, it checks to see if its first word (using the ``getFirst`` method) equals the second word (using the ``getSecond`` method). If there is a match, it increments a counter which it returns at the end of the method. To test this method, add another "there" into the words array and then uncomment the call to ``numMatches``. Summary ----------- -- ArrayLists can be traversed with an enhanced for each loop, or a while or for loop using an index. - +- ``ArrayList``\ s can be traversed with an enhanced ``for`` loop, a ``while`` loop, or a regular ``for`` loop using an index. -- Deleting elements during a traversal of an ArrayList requires using special techniques to avoid skipping elements, since remove moves all the elements down. +- Deleting elements during a traversal of an ``ArrayList`` requires using special techniques to avoid skipping elements, since ``remove`` moves all the elements down. -- Since the indices for an ArrayList start at 0 and end at the number of elements − 1, accessing an index value outside of this range will result in an ArrayIndexOutOfBoundsException being thrown. +- Since the indices for an ``ArrayList`` start at 0 and end at the number of elements − 1, accessing an index value outside of this range will result in an ``IndexOutOfBoundsException`` being thrown. -- Changing the size of an ArrayList while traversing it using an enhanced for loop can result in a ConcurrentModificationException being thrown. Therefore, when using an enhanced for loop to traverse an ArrayList, you should not add or remove elements. +- Changing the size of an ``ArrayList`` while traversing it using an enhanced + ``for`` loop can result in a ``ConcurrentModificationException`` being thrown. + Therefore, when using an enhanced ``for`` loop to traverse an ``ArrayList``, + you should not ``add`` or ``remove`` elements. From 683f476f3e470be1d6c1b3b1cc0ac2c0ec213872 Mon Sep 17 00:00:00 2001 From: Beryl Hoffman Date: Sat, 6 Jan 2024 19:09:21 -0500 Subject: [PATCH 4/8] fix pseudocode formatting --- _sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst index fdf23472..2d31396b 100644 --- a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst +++ b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst @@ -570,7 +570,7 @@ Look at the ``StudentList`` example above for help. } .. figure:: Figures/wordpairs.png - :width: 200px + :width: 180px :align: left :figclass: align-center @@ -583,7 +583,8 @@ how the word pairs are formed. In the class ``WordPairsList`` below, you will write the constructor which takes the array of words and pairs them up as shown in the figure. You will need nested loops to pair each element with the rest of the elements in the list. -Here is the pseudocode. + +Here is the pseudocode for the constructor method. - Initialize the ``allPairs`` list to an empty ``ArrayList`` of ``WordPair`` objects. @@ -591,7 +592,7 @@ Here is the pseudocode. - Loop through the rest of the word array starting from index ``i + 1`` for the second word in the word pair (for loop from index ``j = i + 1`` to ``length``) - - Add the new ``WordPair`` formed from the *i*th word and the *j*th word to the ``allPairs`` ``ArrayList``. + - Add the new ``WordPair`` formed from the ``i``th word and the ``j``th word to the ``allPairs`` ``ArrayList``. .. activecode:: challenge-7-3-WordPairs :language: java From e9fd2ac6821672193fb3af64143ac347b36b4fac Mon Sep 17 00:00:00 2001 From: Peter Seibel Date: Mon, 1 Jan 2024 17:16:34 -0800 Subject: [PATCH 5/8] Small tweaks to 7.3 --- .../topic-7-3-arraylist-loops.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst index 2d31396b..5f3cb75c 100644 --- a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst +++ b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst @@ -178,7 +178,7 @@ While Loop ---------------------- The example below demonstrates a ``while`` loop and an object-oriented approach -where the list is a field of the current object and you use an object method +where the list is a field of the current object and you use an instance method rather than a class (static) method to loop through the list. |CodingEx| **Coding Exercise** @@ -197,9 +197,9 @@ rather than a class (static) method to loop through the list. { private ArrayList nameList; - public ListWorker(ArrayList theNames) + public ListWorker(ArrayList nameList) { - nameList = theNames; + this.nameList = nameList; } public boolean removeName(String name) @@ -762,11 +762,16 @@ the words array and then uncomment the call to ``numMatches``. Summary ----------- -- ``ArrayList``\ s can be traversed with an enhanced ``for`` loop, a ``while`` loop, or a regular ``for`` loop using an index. +- ``ArrayList``\ s can be traversed with an enhanced ``for`` loop, a ``while`` + loop, or a regular ``for`` loop using an index. -- Deleting elements during a traversal of an ``ArrayList`` requires using special techniques to avoid skipping elements, since ``remove`` moves all the elements down. +- Deleting elements during a traversal of an ``ArrayList`` requires using + special techniques to avoid skipping elements, since ``remove`` moves all the + elements above the removed index down. -- Since the indices for an ``ArrayList`` start at 0 and end at the number of elements − 1, accessing an index value outside of this range will result in an ``IndexOutOfBoundsException`` being thrown. +- Since the indices for an ``ArrayList`` start at 0 and end at the number of + elements − 1, accessing an index value outside of this range will result in an + ``IndexOutOfBoundsException`` being thrown. - Changing the size of an ``ArrayList`` while traversing it using an enhanced ``for`` loop can result in a ``ConcurrentModificationException`` being thrown. From bbc64e1333a5d0a88bc5c71d820d1e86353d2353 Mon Sep 17 00:00:00 2001 From: Beryl Hoffman Date: Sat, 6 Jan 2024 19:30:49 -0500 Subject: [PATCH 6/8] Update revised date and amazon scholarship --- _sources/index.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_sources/index.rst b/_sources/index.rst index d91e1fd5..fa168cf2 100644 --- a/_sources/index.rst +++ b/_sources/index.rst @@ -21,7 +21,7 @@ ATTENTION high school women of color taking AP CSA or CSP: if you identify as fe .. ATTENTION high school women, genderqueer, and non-binary technologists: Apply Sept. 1st until Oct. 20th for the **NCWIT Award for Aspirations in Computing** to be recognized for all that you do (or want to do) in technology. Visit http://www.aspirations.org/AiCHSAward for details. -ATTENTION high school seniors: apply for the https://www.amazonfutureengineer.com/scholarships until Dec. 15th for college scholarships and Amazon summer internships for students with financial need. +.. ATTENTION high school seniors: apply for the https://www.amazonfutureengineer.com/scholarships until Dec. 15th for college scholarships and Amazon summer internships for students with financial need. Apply for the ACM Cutler-Bell Scholarship https://csteachers.org/awards/cutlerbell/ until Jan. 19th. @@ -101,7 +101,7 @@ Index If you see errors or bugs, please report them with this |errors form|. If you are a teacher who is interested in CSAwesome PDs or community, please fill out this |interest form| and join the |teaching CSAwesome group| which will give you access to lesson plans at |csawesome|. -(last revised 11/22/2023) +(last revised 1/6/2024) © Copyright 2014-2023 Barb Ericson, Univ. Michigan; 2019-2023 Beryl Hoffman, Elms College; 2023 Peter Seibel, Berkeley High School. All rights reserved. From fe979e7e763861b8adf3a62c0413e7c06bfa1f97 Mon Sep 17 00:00:00 2001 From: Beryl Hoffman Date: Sat, 6 Jan 2024 19:50:25 -0500 Subject: [PATCH 7/8] 7.3 Fix pseudocode formatting one more time --- _sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst index 5f3cb75c..cfe8fabe 100644 --- a/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst +++ b/_sources/Unit7-ArrayList/topic-7-3-arraylist-loops.rst @@ -570,7 +570,7 @@ Look at the ``StudentList`` example above for help. } .. figure:: Figures/wordpairs.png - :width: 180px + :width: 190px :align: left :figclass: align-center @@ -592,7 +592,7 @@ Here is the pseudocode for the constructor method. - Loop through the rest of the word array starting from index ``i + 1`` for the second word in the word pair (for loop from index ``j = i + 1`` to ``length``) - - Add the new ``WordPair`` formed from the ``i``th word and the ``j``th word to the ``allPairs`` ``ArrayList``. + - Add the new ``WordPair`` formed from the ``i``\ th word and the ``j``\ th word to the ``allPairs`` ``ArrayList``. .. activecode:: challenge-7-3-WordPairs :language: java From bda7c54af8f27648064d4b3dfb4924d772bb3b19 Mon Sep 17 00:00:00 2001 From: Peter Seibel Date: Sat, 6 Jan 2024 17:22:23 -0800 Subject: [PATCH 8/8] Update copyright range. --- _sources/index.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_sources/index.rst b/_sources/index.rst index fa168cf2..5b6ef60c 100644 --- a/_sources/index.rst +++ b/_sources/index.rst @@ -103,7 +103,7 @@ If you see errors or bugs, please report them with this |errors form|. If you ar (last revised 1/6/2024) -© Copyright 2014-2023 Barb Ericson, Univ. Michigan; 2019-2023 Beryl Hoffman, -Elms College; 2023 Peter Seibel, Berkeley High School. All rights reserved. +© Copyright 2014-2024 Barb Ericson, Univ. Michigan; 2019-2024 Beryl Hoffman, +Elms College; 2023-2024 Peter Seibel, Berkeley High School. All rights reserved. Created using `Runestone `_.