Java List and Arraylist Master

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Why do Arrays and ArrayList exist in the language?

- Arrays are Java's fundamental data storage - ArrayList is a library built on top of an array When would you choose an array over an ArrayList? - When you need a fixed size that you know ahead of time - Simpler syntax for getting/setting -More efficient - Multi-dimensional arrays (e.g. images) - Histograms/tallying

Methods of List<E> interface void add(int index, E element)

- Inserts element at specified index. - elements from position index and higher have 1 added to their indices. - Size of list is incremented by 1

Methods of List<E> interface E remove(int index)

- Removes and returns the element at the specified index. - elements to the right of position index have 1 subtracted from their indices. - size of list is decreased by 1

Methods of List<E> interface boolean add(E obj)

- appends obj to the end of the list. - Always returns true - If the specified element is not of the type E ClassCastException is thrown

ArrayList methods list.add(value);

- appends value at end of list List<Integer> list = new ArrayList<Integer>() //An ArrayList is-a List for (inti=0; i<4; i++) { list.add(i); //example of auto-boxing } // i is wrapped in an Integer before insertion

Methods of List<E> interface int size()

- returns no of elements in the list.

Methods of List<E> interface E get(int index)

- returns the element at the specified index

Demonstrate how to: 1. Add 2. Change (edit), and 3. Delete (remove) an item from an ArrayList using our GroceryList example! CLASS EXAMPLE: private ArrayList<String> groceryList = new ArrayList<String>();

1. ADD AN ITEM: public void addGroceryItem(String item) { groceryList.add(item); } 2. EDIT/CHANGE AN ITEM: public void modifyGroceryItem(int position, String newItem) { groceryList.set(position, newItem); System.out.println("Grocery item " + position+1 + " has been modified"); } 3. REMOVE AN ITEM: public void removeGroceryItem(int position) { groceryList.remove(position); } NOTE: Ideally- all of these method would be optimized for the sake of the customer/user. It's unlikely that the user would know the INDEX POSITION of the items on their grocery list - but would rather search for them by NAME.

A list allows you to

1. Access an element at any position in the list using its integer index 2. Insert an element janywhere in the list 3. Iterate over all elements using ListIterator or Iterator

What are 2 ways we can copy one ArrayList to another?

1. METHOD ONE: - Add a getter method for your class file (GroceryList) - Add another case to your switch statement with a new method: processArrayList(); - Create the method like this: public static void processArrayList() { ArrayList<String> newArray = new ArrayList<String>(); newArray.addAll(groceryList.getGroceryList()); } The addAll() method simply selects all the items/elements that are in the groceryList array and assigns them to newArray. Simple! This way you don't have to create a for loop- it's extra code and extra computing power. 2. METHOD 2: SHORTCUT This is even easier! If you know the name of the ArrayList that you are going to be copying, you can actually copy it WITHIN the new ArrayList's instantiation: public static void processArrayList() { ArrayList<String> nextArray = new ArrayList<String>(groceryList.getGroceryList()); }

How do you create (initialize) an ArrayList? How is the declaration DIFFERENT than an array?

1. private ArrayList<String> groceryList = new ArrayList<String>(); NOTE: you add the parentheses AFTER the <>... ArrayList<String>() Because the ArrayList IS a class of it's own- meaning it has it's own CONSTRUCTOR. 2. You don't define the data type in the same way BECAUSE an ArrayList contains objects: ARRAY EXAMPLE: private int[] myArray;

Consider writing a program that produces statistics for long lists of numerical data. Which of the following is the best reason to implement each list with an array of int (or double), rather than an ArrayList of Integer (or Double) objects? A. An array of primitive number types is more efficient to manipulate than an ArrayList of wrapper objects that contain numbers. B. Insertion of new elements into a list is easier to code for an array than for an ArrayList C. Removal of elements from a list is easier to code for an array than for an ArrayList. D. Accessing individual elements in the middle of a list easier for an array than for an ArrayList. E. Accessing all the elements is more efficient in an array than in an ArrayList

A In order to add numerical elements to be added to an ArrayList, each element must be wrapped in a wrapper class before insertion into the list. Then to retrieve a numerical value from the ArrayList, the element must be unboxed using the intValue or doubleValue methods, Even though these operations cab be taken care of with auto-boxing and auto-unboxing there are efficiency costs. In an array, you simply use the [] notation for assignment (as in arr[i] = num) or retrieval (value = arr[i]) B and C are false statements, Both insertion and deletion for an array involve writing code to shift elements. An ArrayList automatically takes care of this through its add and remove methods. Choice D is a poor reason for choosing an array. While the get and set methods of ArrayList might be slightly more aekward than using the [] notation, both ,mechanism work pretty easily. Choice E is false. Efficiency of access is roughly the same.

To save something to an ArrayList, it MUST be...

A class object: This means that you CANNOT create an ArrayList JUST by declaring a primitive type: WRONG EXAMPLE: ArrayList<int> intArrayList = new ArrayList<int>(); CORRECT EXAMPLE: Use a wrapper class! ArrayList<Integer> intArrayList = new ArrayList<Integer>();

What is the matrix?

A matrix is an array if row arrays The number of rows is mat.length The number of columns is mat[0].length

What is a static method? What other name(s) is it known by?

A method by which we access the CLASS (Contact- not the "contact" object) but not an OBJECT. It allows us to CALL a method on the CLASS without having to instantiate a new object: METHOD EXAMPLE: public static Contact createContact(String name, String phoneNumber) { return new Contact(name, phoneNumber); } CALLING THE METHOD: Contact newContact = Contact.createContact(name, phone); These are also known as "convenience" or "factory" methods.

Erasure

All of the type information in a program with generic classes is examined at compile time. After compilation the type information is erased. This feature of generic classes is known as erasure. during execution of the program, any attempt at incorrect casting or comparisons will lead to run-time errors.

What are Lists and Arraylists in Java? What is the relationship between Lists and Arraylists?

An "ordered collection (a sequence) of elements. The user of this interface has precise control over where in the list each element is inserted." An Arraylist inherits (extend) from List (technically, from an AbstractList). List also extends "Collection".

What is an ArrayList?

An ArrayList is essentially a RESIZEABLE array! As you add elements to an ArrayList, it automatically resizes itself. It's storage capacity grows automatically! It automatically maintains the size of the list and the amount of memory that it needs for storage. Because the ArrayList is a CLASS- it can have methods (functions) run on it which will add/delete values to it.

Definition of an array

An array is a sequence of values of the same type. They can hold both primitives and objects.

What does an array variable do?

An array variable stores a reference to the array. Copying the variable yields a second reference to the same array.

Definition of an Iterator

An iterator is an object whole sole purpose is to traverse a collection, one element at a time. During iteration, the iterator object maintains a current position in the collection, and is the controlling object in manipulating the elements of the collection.

ArrayList ArrayList<Integer> list = new ArrayList<>(); list.add(1); // [1] list.add(2); // [1, 2] list.set(0, 3); // [3, 2] int x = list.get(0); // 3 list.add(4); // [3, 2, 4] list.contains(2); // true

Array int[] arr = new int[2]; // [0, 0] arr[0] = 1; // [1, 0] arr[1] = 2; // [1, 2] arr[0] = 3; // [3, 2] int x = arr[0]; // 3 [no equivalent]

What makes an array list different from an array?

Array vs ArrayList in Java 1) Array is a fixed length data structure while ArrayList is a variable length Collection class. 2) Array - you cannot use Generics (as Array instance knows about what kind of type it can hold and throws ArrayStoreException) ArrayList allows you to use Generics to ensure type-safety. 3) Array - length variable, which denotes length of Array while ArrayList - size() method to calculate size of ArrayList in Java. 4) ArrayList - you can not store primitives, it can only contain Objects. Array can contain both primitives and Objects in Java. Though Autoboxing of Java 5 may give you an impression of storing primitives in ArrayList, it actually automatically converts primitives to Object. The numbers must be boxed in wrapper classes like Integer and Double before insertion into ArrayList e.g. ArrayList<Integer> integerList = new ArrayList<Integer>(); integerList.add(1); //here we are not storing primitive in ArrayList, instead autoboxing will convert int primitive to Integer object 5) Java provides add() method to insert element into ArrayList and you can simply use assignment operator to store element into Array e.g. In order to store Object to specified position use Object[] objArray = new Object[10]; objArray[1] = new Object(); 6) You can create instance of ArrayList without specifying size, Java will create Array List with default size (By the way you can also initialize ArrayList while creating it) but its mandatory to provide size of Array while creating either directly or indirectly by initializing Array while creating it.

IndexOutofBoundsException

ArrayList methods - add, get, remove and set throws this exception if index is out of range. For get, remove and set, index is out of range if index <0 || index >= size() For add, however, it is ok to add an element at the end of the list. Thereforeindex is out of range if index <0 || index > size()

Syntax for constructing an ArrayList?

ArrayList<type> reference = new ArrayList<type>();

Finding max or min algorithm.

BankAccount largestYet = accounts.get(0); for (int i = 1; i < accounts.size(); i++) { BankAccount a = accounts.get(i); if (a.getBalance() > largestYet.getBalance()) largestYet = a; } return largestYet;

Why is there a "+1" in this code for printing out our GroceryList AFTER the "i" (index position)? public void printGroceryList() { System.out.println("You have " + groceryList.size() + " items in your grocery list"); for(int i=0; i <groceryList.size(); i++) { System.out.println((i+1) + ": " + groceryList.get(i)); } }

Because the computer counts from 0. The first index position in your "grocery list" is 0, but if a HUMAN was making a grocery list, the first item will be number "1". A LOT of your code will be corrected in this way to make it "human readable". OTHER EXAMPLES: public void modifyGroceryItem(int position, String newItem) { groceryList.set(position, newItem); System.out.println("Grocery item " + position+1 + " has been modified"); } INVERSE EXAMPLE: NOTE: When the USER is modifying something, we have to do the opposite: add a -1 to access the correct index position to remove the item! public static void removeItem() { System.out.print("Enter item number: "); int itemNumber = scanner.nextInt(); scanner.nextLine(); groceryList.removeGroceryItem(itemNumber-1); }

What does this do? double[] data = new double[10]; . . . // Fill array double[] prices = data;

Both arrays are now pointing to the same reference.

What is auto-boxing?

Conversion between primitive types and the corresponding wrapper classes is automatic.

How would you print out the list of all the items in your ArrayList? Create a numbered printout using our GroceryList class: CLASS EXAMPLE: public class GroceryList { private ArrayList<String> groceryList = new ArrayList<String>();

Create a method! public void printGroceryList() { System.out.println("You have " + groceryList.size() + " items in your grocery list"); for(int i=0; i <groceryList.size(); i++) { System.out.println((i+1) + ": " + groceryList.get(i)); } }

Consider Writing a program that reads the lines of any text file into a sequential list of lines. Which of the following is a good reason to implement the list with an ArrayList of String objects rather than an array of String objects ? A. The get and set methods of ArrayList are more convenient than the [] notation for arrays. B. The size method of ArrayList provides instant access to the length of the list C. An ArrayList can contain objects of any type, which leads to greater generality. D. If any particular text file is unexpectedly long, the ArrayList will automatically be resized, The array, by contrast, may go out of bounds. E. The String methods are easier to use with an ArrayList than with an array.

D. Arrays are of fixed lengths and do not shrink or grow if the size of the data set varies. An ArrayList automatically resizes the list. Choice A is false. The [] notation is compact and easy to use. Choice B is not a valid reason because an array arr also provides instant access to its length with the quantity arr.length Choice C is invalid because an array can also contain objects. Also generality is beside the point in the given program - the list must hold String objects. Choice E is false. Whether a String object is arr[i] or list.get(i), the String methods are equally easy to invoke.

Iterator<E> interface methods void remove()

Deletes from the collection the last element that was returned by next this method can be called only once per call to next. It throws an IllegalStateException if the next method has not yet been called, or if the remove method has already been called after the last call to next.

ArrayList is a ___ class.

Generic class. The collections classes are generic with parameters. List<E> and ArrayList<E> contain elements of type E.

Disadvantage of arrays

Insertion and deletion of an element in an ordered list is inefficient, since, in the worst case, it may involve moving all the elements in he list.

You want to make sure that your methods are optimized for the user: In our first run through with this code (our addItem method for "GroceryList"), when the user was using our application, they were required to enter in the NUMBER of the item on the grocery list (the method required this input). This is a bit of a pain for the user: How would we optimize our code for the user? (HINT: how would a USER - a person- enter or search for their items on a groceryList)?

It doesn't make sense to make the user enter a NUMBER for an item on their groceryList - most people aren't going to know (or care about) the index position of a specific item on their list: they are going to know the NAMES of the items. Mostly- you will be: - removing all the references to the item number - changing the scanner method from nextInt() to nextLine() - Making sure any saved values are also changed from "int" to "String". There! Much easier for the user! EXAMPLE: public static void addItem() { System.out.print("Please enter the grocery item: "); groceryList.addGroceryItem(scanner.nextLine()); } public static void modifyItem() { System.out.print("Enter item name: "); String item = scanner.nextLine(); System.out.print("Enter new item name: "); String newItem = scanner.nextLine(); groceryList.modifyGroceryItem(item, newItem); } public static void removeItem() { System.out.print("Enter item name: "); String item = scanner.nextLine(); groceryList.removeGroceryItem(item); } public static void searchForItem() { System.out.print("Item to search for: "); String searchItem = scanner.nextLine(); if(groceryList.onFile(searchItem)) { System.out.println("Found " + searchItem + " in our grocery list!"); } else { System.out.println(searchItem + " is not in the shopping list!"); } }

How to Loop ArrayList in Java?

Iterating, traversing or Looping ArrayList in Java means accessing every object stored in ArrayList and performing some operations like printing them. There are many ways to iterate, traverse or Loop ArrayList in Java e.g. advanced for loop, traditional for loop with size(), By using Iterator and List Iterator along with while loop etc. import java.util.ArrayList; import java.util.Iterator; /** * Java program which shows How to loop over ArrayList in Java using advanced for loop, * traditional for loop and How to iterate ArrayList using Iterator in Java * advantage of using Iterator for traversing ArrayList is that you can remove * elements from Iterator while iterating. * @author */ public class ArrayListLoopExample { public static void main(String args[]) { //Creating ArrayList to demonstrate How to loop and iterate over ArrayList ArrayList<String> games = new ArrayList<String>(10); games.add("Cricket"); games.add("Soccer"); games.add("Hockey"); games.add("Chess"); System.out.println("original Size of ArrayList : " + games.size()); //Looping over ArrayList in Java using advanced for loop System.out.println("Looping over ArrayList in Java using advanced for loop"); for(String game: games){ //print each element from ArrayList System.out.println(game); } //You can also Loop over ArrayList using traditional for loop System.out.println("Looping ArrayList in Java using simple for loop"); for(int i =0; i<games.size(); i++){ String game = games.get(i); } //Iterating over ArrayList in Java Iterator<String> itr = games.iterator(); System.out.println("Iterating over ArrayList in Java using Iterator"); while(itr.hasNext()){ System.out.println("removing " + itr.next() + " from ArrayList in Java"); itr.remove(); } System.out.println("final Size of ArrayList : " + games.size()); } } Output: original Size of ArrayList : 4 Looping over ArrayList in Java using advanced for loop Cricket Soccer Hockey Chess Looping ArrayList in Java using simple for loop Iterating over ArrayList in Java using Iterator removing Cricket from ArrayList in Java removing Soccer from ArrayList in Java removing Hockey from ArrayList in Java removing Chess from ArrayList in Java final Size of ArrayList : 0 In summary use advance for loop to loop over ArrayList in Java, its short, clean and fast but if you need to remove elements while looping use Iterator to avoid ConcurrentModificationException.

Assume a list of integer strings write a program to remove all occurences of "6" from the list.

Iterator<String> itr = list.iterator(); while(itr.hasNext()) { String num = itr.next(); if(num.equals("6")) { itr.remove(); System.out.println(list); } } if the original list is 2 6 6 3 5 6, the o/p will be [2, 6, 3, 5, 6] [2, 3, 5, 6] [2, 3, 5]

Methods of List<E> interface E set(int index, E element)

Replaces item at the specified index in the list with specifies element. - returns the element that was previously at index. - - If the specified element is not of the type E ClassCastException is thrown

Methods of List<E> interface Iterator<E> iterator()

Returns an iterator over the elements in the list, in proper sequence, starting at the first element.

Arrays as Parameters

Since arrays are treated as objects, passing an array as a parameter means passing its object reference. No copy is made of the array. Thus the elements of the actual array can be accessed and modified.

How to sort ArrayList ascending descending order

Sorting ArrayList in Java is not difficult, by using Collections.sort() method you can sort ArrayList in ascending and descending order in Java. Collections.sort() method optionally accept a Comparator and if provided it uses Comparator's compare method to compare Objects stored in Collection to compare with each other, in case of no explicit Comparator, Comparable interface's compareTo() method is used to compare objects from each other. If object's stored in ArrayList doesn't implements Comparable than they can not be sorted using Collections.sort() method in Java. import java.util.ArrayList; import java.util.Collections; /** * * Java program to demonstrate How to sort ArrayList in Java in both ascending * and descending order by using core Java libraries. * * @author Javin */ public class CollectionTest { public static void main(String args[]) { //Creating and populating ArrayList in Java for Sorting ArrayList<String> unsortedList = new ArrayList<String>(); unsortedList.add("Java"); unsortedList.add("C++"); unsortedList.add("J2EE"); System.err.println("unsorted ArrayList in Java : " + unsortedList); //Sorting ArrayList in ascending Order in Java Collections.sort(unsortedList); System.out.println("Sorted ArrayList in Java - Ascending order : " + unsortedList); //Sorting ArrayList in descending order in Java Collections.sort(unsortedList, Collections.reverseOrder()); System.err.println("Sorted ArrayList in Java - Descending order : " + unsortedList); } } Output: unsorted ArrayList in Java : [Java, C++, J2EE] Sorted ArrayList in Java - Ascending order : [C++, J2EE, Java] Sorted ArrayList in Java - Descending order : [Java, J2EE, C++] Just remember that Collections.sort() will sort the ArrayList in ascending order and if you provide reverse comparator it will sort the ArrayList in descending order in Java. In this Sorting we have used Comparable method of String for sorting String on there natural order, You can also use Comparator in place of Comparable to sort String on any other order than natural ordering e.g. in reverse order by using Collections.reverseOrder() or in case insensitive order by using String.CASE_INSENSITIVE_COMPARATOR.

Let's write a program that reverses a text file.

String filename = promptUserForFile("Filename: ", "res"); try { Scanner s = new Scanner(new File(filename)); ArrayList<String> lines = new ArrayList<>(); // Read all lines and store in our ArrayList while (scanner.hasNextLine()) { lines.add(scanner.nextLine()); } // Output the lines from back to front for (int i = lines.size() - 1; i >= 0; i--) { println(lines.get(i)); } } catch (IOException ex) { println("Could not read file."); }

Which interface does ArrayList implement ?

The List Interface.

What is the index of the first element is List<E> interface?

The elements of the list are indexed with 0 being the index of the first element

What does an enhanced for loop (for-each) do?

The enhanced for loop traverses all elements of a collection. You can't assign values with a for each loop.

What field returns the number of elements in an array?

The length field. It is an instance variable of the array.

Iterator<E> interface

The package java.util provides a generic interface, Iterator<E>

We want our logic (the main FUNCTIONALITY) of the application...

To be hidden, and separate from our front-end! The main functionality of the application (the BACK-END logic) should all be in the CLASS FILE- NOT the "Main" file. The code we see there- in the Main file - should be (as much as possible) the "Front end", user interaction kind of code.

T/F Duplicate elements allowed in a list.

True

T/F Every call to remove must be preceded by next.

True

T/F Use a for-each loop for accessing and modifying objects in a list

True

T/F Use an iterator for removal of objects

True

What is a 2D array?

Two-dimensional arrays form a tabular, two-dimensional arrangement. You access elements with an index pair a[i][j].

Does auto-boxing work inside arithmetic expressions?

Yes.

Do you have to import the ArrayList class to use it?

Yes. import java.util.ArrayList

Is ArrayList a class in the Collections API ?

Yes. ArrayList class is in the Collections Application Programming Interface. Most of the API is in java.util All collections classes, including ArrayList, have the following features. 1. Memory and Run-time efficient. 2. provide methods for insertion and removal of items (ie. they can grow and shrink) 3. They provide for iteration over the entire collection.

When you construct an ArrayList object, it has a size of?

Zero

How to find the number of elements in: a) an Array b) an ArrayList c) a String

a) .length b) .size() c) .length()

How do you get the last valid index of an ArrayList?

arrayListReference.size() - 1.

Syntax for array element access

arrayReference[index] Ex. data[2]

How do you access the last index of an array?

arrayReference[length - 1]

ArrayList() - what does this do ?

constructs an empty list.

Syntax for creating an array with 3 rows and 3 columns.

final int ROWS = 3; final int COLUMNS = 3; String[][] board = new String [ROWS][COLUMNS];

Syntax of a for-each loop.

for (Type variable : collection) statement Example: for (double e : data) sum = sum + e;

How to fill or search a 2D array.

for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLUMNS; j++) board[i][j] = " ";

How don you traverse an ArrayList of Integer

for(Integer num : list) System.out.println(num); //prints the elements of list, one per line

ArrayList methods list.add(index, value);

inserts given value just before the given index, shifting subsequent values to the right list.add(1,7) // list is 0 7 1 5 list.add(2, 8) // list is 0 7 8 1 5

What methods do you use to retrieve the numerical value of an Integer (or Double) stored in ArrayList ?

intValue() (or doubleValue() must be invoked (unwrapping).

Initialize an integer array named primes with five values.

int[] primes = new int[] { 2, 3, 5, 7, 11 };

Auto-unboxing

it is the automatic conversion of a wrapper class to its corresponding primitive type If a program tries to auto-unbox null, NullPointerException is thrown.

How would you find an item in your ArrayList? Use our GroceryList example: CLASS EXAMPLE: public class GroceryList { private ArrayList<String> groceryList = new ArrayList<String>();

public String findItem(String searchItem) { // boolean exists = groceryList.contains(searchItem); int position = groceryList.indexOf(searchItem); if(position >=0) { return groceryList.get(position); } return null; } You could manually type out code similar to what we had in our "printGroceryList" method- a for loop that goes through and looks at each item... public void printGroceryList() { System.out.println("You have " + groceryList.size() + " items in your grocery list"); for(int i=0; i <groceryList.size(); i++) { System.out.println((i+1) + ": " + groceryList.get(i)); } } ...but that would be a waste of time because there are built in methods for this!

Finding a value algorithm.

public class Bank { public BankAccount find(int accountNumber) { for (BankAccount a : accounts) { if (a.getAccountNumber() == accountNumber) // Found a match return a; } return null; // No match in the entire array list } . . . }

Counting matches algorithm.

public class Bank { public int count(double atLeast) { int matches = 0; for (BankAccount a : accounts) { if (a.getBalance() >= atLeast) matches++; // Found a match } return matches; } . . . private ArrayList<BankAccount> accounts; }

How would you take the contents of an ArrayList and convert them back to a regular array? Use our groceryList example.

public static void processArrayList() { String[] myArray = new String[groceryList.getGroceryList().size()]; myArray = groceryList.getGroceryList().toArray(myArray); }

ArrayList methods list.clear();

removes all elements of the list

ArrayList methods list.remove(value);

removes the first occurrence of the value, if any

ArrayList methods list.remove(index);

removes/returns value at given index, shifting subsequent values to the left x = list.remove(2) //list is now 0 1 5 - x contains Integer with value 4

ArrayList methods list.set(index, value);

replaces value at given index with given value Integer x = list.set(3,5); //list is 0 1 4 5 - x contains Integer with value 9

ArrayList methods list.toString()

returns a string representation of the list such as "[3, 42, -7, 15]"

ArrayList methods list.indexOf(value)

returns first index where given value is found in list (-1 if not found)

Iterator<E> interface methods E next()

returns the next element in the iteration. if no elements remain, the method throws a NoSuchElementException

ArrayList methods list.size()

returns the number of elements in the list E.g. public static void insert(List<Integer> list, Integer value) { int index = 0; while(index < list.size() && value.compareTo(list.get(index)) > 0) index++; list.add(index, value); //insert value } //Suppose value is larger than all the elements in the list. The insert method will throw an IndexOutOfBoundsException if the index<list.size() is omitted

ArrayList methods list.get(index)

returns the value at given index Integer intOb = list.get(2); // assigns Integer with value 4 to intOb - leaves list unchanged int n = list.get(3); // example of auto-unboxing - Integer is retrieved and converted to int - n contains 9

ArrayList methods list.isEmpty()

returns true if the list contains no elements

Iterator<E> interface methods boolean hasNext()

returns true if there's at least one more element to be examined, false otherwise

Syntax for initializing an array

typeName[] arrayReference = new typeName[length] Ex. double[] data = new double[10];

Why do you have () after the carrots <String> in the ArrayList instantiation? EXAMPLE: private ArrayList<String> groceryList = new ArrayList<String>();

you add the parentheses AFTER the <>... ArrayList<String>() Because the ArrayList IS a class of it's own- meaning it has it's own CONSTRUCTOR. SIDENOTE: You may have an ArrayList as a "field" within your "main" class and have the instantiation done in that declaration.


संबंधित स्टडी सेट्स

Ch.59 Assessment and Management of Patients with Male Reproductive Disorders

View Set

The Federal Deposit Insurance Corporation (FDIC)

View Set

Section Two: Producer Responsibilities

View Set

Med Surg Exam #3 - Book NCLEX Questions

View Set

Microbiology - Exam 4 - Chapters 21, 22, & 23

View Set

The Parathyroid Glands, Function and Dysfunction + Glands/Tumours Pathology

View Set

Analysis: Fundamental Analysis Review Questions

View Set