An ArrayList stores a variable number of objects. This is similar to making an array of objects, but with an ArrayList, items can be easily added and removed from the ArrayList and it is resized dynamically. This can be very convenient, but it's slower than making an array of objects when using many elements. Note that for resizable lists of integers, floats, and Strings, you can use the Processing classes IntList, FloatList, and StringList. Show An ArrayList is a resizable-array implementation of the Java List interface. It has many methods used to control and search its contents. For example, the length of the ArrayList is returned by its size() method, which is an integer value for the total number of elements in the list. An element is added to an ArrayList with the add() method and is deleted with the remove() method. The get() method returns the element at the specified position in the list. (See the above example for context.) For a list of the numerous ArrayList features, please read the Java reference description. The following problems are similar to what you might see on the AP CS A exam. Please answer each to the best of your ability. 12-4-1: Consider the following segment of code. For the method call 12-4-2: Consider the following class declarations. Which of the following code can be executed in the public class Bird { private String color; public Bird(String theColor) { /* implementation not shown */ } public void makeNoise() { /* implementation not shown */ } public void eat() { /* implementation not shown */ } public string showFeathers() { return color; } } public class Swan extends Bird { /* no constructors or other methods have been declared */ } I. this.color = "blue"; II. eat(); III. Swan s = new Swan("blue"); 12-4-3: Consider the following code. Assume that list.add(4); list.add(1, 8); list.remove(2); list.set(2, 1); list.add(3); 12-4-4: The method public int[] rowSums(int[][] arr) { int[] ans = new int[arr.length]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { /* to be determined */ } } return ans; } 12-4-5: Consider the following method public static int binSearch(int key, int[] arr) { int low = 0; int high = arr.length - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] == key) return mid; else if (arr[mid] < key) low = mid + 1; else high = mid - 1; } return -1; } 12-4-6: The public static String wordScramble (String str) { if (str.length() == 0) return "!"; else return wordScramble(str.substring(1)) + str.substring(0,1); } 12-4-7: Which of these loops will print multiples of 10, from 0 to 100 inclusive? I. for (int i = 0; i < 11; i++) { System.out.print(i * 10 + " "); } II. int i = 0; while (i <= 10) { System.out.print(i * 10 + " "); i++; } III. for (int i = 0; i <= 100; i += 10) { System.out.print(i + " "); } 12-4-8: The public class Person { private String name; private int age; public Person(String theName, int theAge) { name = theName; age = theAge; } } public class Student extends Person { private int grade; public Student(String theName, int theAge, int theGrade) { /* to be completed */ } } I. name = theName; age = theAge; grade = theGrade; II. super(theName, theAge); grade = theGrade; III. super(theName, theAge); name = theName; age = theAge; grade = theGrade;
12-4-9: A list of integers containing 12-4-10: Consider the classes public class Animal { private String name; public Animal(String theName) { name = theName; } public Animal() { name = "Animal"; } public String makeNoise() { return ""; }; public String getName() { return name; } } public class Pig extends Animal { public Pig(String theName) { super(theName); } public String makeNoise() { return "Oink!"; } public String getName() { return "My name is " + super.getName() + "!"; } public static void main(String[] args) { Animal piglet = new Pig("Piglet"); System.out.print(piglet.getName()); } } 12-4-11: Consider the following
method oddArray, which changes every even number value in the array to 0. By the end of the method, only odd numbers will be present in the array. Which line correctly completes public void oddArray (int[] arr) { for (int i = 0; i < arr.length; i++) { //if the number at arr[i] is even, it becomes 0 if( /* to be determined */ ) arr[i] = 0; } } 12-4-12: The method public static int numFun(int num) { if (num / 10 == 0) return num; else return (num % 10) + numFun(num / 10); } 12-4-13: Consider the method public boolean emptyList (List <Integer> list) { /* to be completed */ } // I. for (int i = 0; i < list.size(); i++) { if (list.get(i) != 0) return false; } return true; // II. for (int i = 0; i < list.size(); i++) { if (list[i] != 0) return false; } return true; // III. for (int i = 0; i < list.size(); i++) { if (list.get(i) != 0) return true; } return false;
12-4-14: You need to find a random integer in the range 1 to 25, inclusive. Which of the following always returns a value that satisfies this condition?
12-4-15: A list of 120 names has been sorted in alphabetical order. Using a binary search method, what is the minimum number of passes needed to confirm that a name is not in the list? 12-4-16: The method public void recur (String str) { if (str.length() < 15) System.out.print("s"); recur(str + "!"); } 12-4-17: Consider the public class Fruit { private String name; private boolean seeds; public Fruit(String theName) { name = theName; seeds = true; } public void setSeeds() { seeds = !seeds; } } public class Grape extends Fruit { private String color; public Grape(String theName, String theColor) { super(theName); color = theColor; } } public class SeedlessGrape extends Grape { public SeedlessGrape(String theName, String theColor) { super(theName, theColor); setSeeds(); } } I. Fruit a = new SeedlessGrape("grape", "red"); II. Grape b = new Grape("grape"); III. SeedlessGrape c = new Grape("grape", "green"); 12-4-18: The method public void printNames (String[][] arr) { for (String[] row : arr) { for (String x : row) { /* to be determined */ } System.out.println(); } }
12-4-19: Which of the following is equivalent to 12-4-20: Consider the method public int mystery (int num) { if (num == 1) return 3; else return 3 * mystery (num - 1); } You have attempted of activities on this page Which of the following method calls will return the number of elements in the ArrayList?The size() method of java. util. ArrayList class is used to get the number of elements in this list. Returns Value: This method returns the number of elements in this list.
What method returns the element at index i in an ArrayList?The indexOf() method is used to get the index of the first occurrence of an element in an ArrayList object. The element whose index is to be returned.
What is the return type of get method in ArrayList?The get() method of ArrayList in Java is used to get the element of a specified index within the list. Parameter: Index of the elements to be returned. It is of data-type int. Return Type: The element at the specified index in the given list.
What does .size return for an ArrayList?size() Return Value
returns the number of elements present in the arraylist.
|