Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Array lists are objects that, like arrays, provide you the ability to store item

ID: 3688657 • Letter: A

Question

Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that the list is empty: [ ]. Create a new NetBeans project with the skeleton program shown above. Then, complete the following tasks by adding code to this skeleton program. If you are asked to print a value, provide a suitable label to identify it when it is printed. Invoke the ArrayList method add (string s) to enter the following names in sequence: Alice, Bob, Connie, David, Edward, Fran, Gomez, Harry. Print the ArrayList again. Use the ArrayList method string get (int index) to retrieve and print the first and last names. Remember that the first name is at index 0, the last name at the index = list size - 1 Print the size () of the ArrayList. Use ArrayList method int size () to help you print the last name in the list. Use ArrayList method string set (int index, string name) to change "Alice" to "Alice B. Toklas". Print the ArrayList to verify the change. Use the alternate form of the ArrayList method add (int index, string name) to insert "Doug" after "David". Print the ArrayList again. Use a for-each loop to print each name in the ArrayList. Create a second ArrayList called names2 that is built by calling the ArrayList constructor that accepts another ArrayList as an argument. Pass names to the constructor to build names2. Then print the ArrayList. Call ArrayList method string names.remove (0) to remove the first element. Print names and names2. Verify that Alice B. Toklas was removed from names, but not from names2.

Explanation / Answer

ArrayListDemo.java

import java.util.*;

public class ArrayListDemo {
public static void main(String args[]) {
// create an array list
ArrayList<String> al=new ArrayList<String>();//The ArrayList class extends AbstractList and
//implements the List interface


//string type generic


// add elements to the array list
al.add("Alice");
al.add("Bob");
al.add("Connie");
al.add("David");
al.add("Edward");
al.add("Fran");
al.add("Gomez");
al.add("Harry");
System.out.println("Contents of al: " + al);
System.out.println("Size of al after additions: " + al.size());
int size=al.size();//sizeof the arraylist
// display the array list

// Remove elements from the array list
String firstname=(String) al.get(0);//this method get the first name with index 0
System.out.println("first name="+firstname);
String lastname=(String) al.get(size-1);//it retrives lastname
System.out.println("lastname="+lastname);
System.out.println("array list size="+size);
lastname=(String) al.get(size-1);
System.out.println("lastname="+lastname);
al.set(0,"Alice B Toklas");//changing oth index name to Alice b Toklas
System.out.println("array llist setting another name at index 1"+al);
al.add(4, "doug");//inserting doug after david
System.out.println(" adding doug after david "+al);

for (String object: al) {//for each which prints arraylist values
System.out.println(object);
}
al.remove(0);//removes oth index
ArrayList<String> names2 = (ArrayList<String>)al.clone();//copying al values to names2
ArrayList<String> names = (ArrayList<String>)al.clone();//copying al values to names

Collections.copy(al,names2);
System.out.println("names2="+names2);
Collections.copy(al,names2);
System.out.println("names="+names);

}

}

output

run:
Contents of al: [Alice, Bob, Connie, David, Edward, Fran, Gomez, Harry]
Size of al after additions: 8
first name=Alice
lastname=Harry
array list size=8
lastname=Harry
array llist setting another name at index 1[Alice B Toklas, Bob, Connie, David, Edward, Fran, Gomez, Harry]
adding doug after david [Alice B Toklas, Bob, Connie, David, doug, Edward, Fran, Gomez, Harry]
Alice B Toklas
Bob
Connie
David
doug
Edward
Fran
Gomez
Harry
names2=[Bob, Connie, David, doug, Edward, Fran, Gomez, Harry]
BUILD SUCCESSFUL (total time: 0 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote