Overview
This lab will continue our exploration of the ArrayList data structure. Specifically, we will cover the topics below:
Remember from the previous lab that in order to use an
ArrayList, you first have to import the class:
import java.util.ArrayList;
You can then create a new ArrayList object:
ArrayList<Object> listTest = new
ArrayList<Object>();
The Java API has a list of all the methods provided by an
ArrayList. See: Java ArrayList API.
Assignment Setup
To begin this lab create a new Java project in eclipse called "ArrayPractice". Within your new project, download the file below and put it in the src folder.
Your Task
In the previous lab we learned about why ArrayLists are useful and started working with them. In this lab we will use an ArrayList to store String objects in order to learn what we can and can not do with them. Follow the steps outlined below to complete this lab. Your lab instructor will explain to you why some things work and why others do not.
import java.util.ArrayList; // Import the ArrayList class
public class ArrayListPractice {
public static void main(String[] args) {
// 1. Create an ArrayList object that holds Strings, initialize the capacity to size 10
ArrayList<> solarSystem = new ArrayList<>();
// 2. Add a String for each of the first five planets to the solarSystem ArrayList
solarSystem.add(planet_name_here);
// 3. Below is an example of a for-each loop, it will print the entire contents of your ArrayList so far
for (String s : solarSystem)
System.out.print(s + ", ");
System.out.println();
// 4. At index 3 insert the String containing the name of our planets only natural satellite
solarSystem.add(index_here, planet_name_here);
// 5. Use a for-each loop to print the entire contents of your solarSystem ArrayList again below
// 6. Now add our local star and the collective group of celestial objects between Mars and Jupiter at their respective indices
// 7. The code line below tries to print the element at index 8, why doesn't it work, you initialized the capacity to size 10 right?
// Type your answer here then comment out the line below:
System.out.println(solarSystem.get(8));
// 8. Add the remaining 2 planets that come after Jupiter to your ArrayList, go ahead and add Pluto as well
// 9. What is the size of your arraylist now? Print a line that uses the .size() method on your ArrayList
// 10. How is the size bigger then the initial capacity you provided? What happened?
// Answer:
// 11. Another ArrayList, solarSystem2 that is a "copy" of your first solarSystem ArrayList
ArrayList<String> solarSystem2 = solarSystem;
// 12. Print solarSystem2 with a for each loop, is it the same as solarSystem?
// Answer:
// 13. Remove Pluto from the solarSystem ArrayList, it's not really a planet anyway right?
// 14. Print solarSystem2, has it changed, why?
// Answer:
// 15. Remove the first seven elements in the solarSystem ArrayList with a for loop, careful you are removing the right indices!
// 16. Make another ArrayList of Strings called solarSystem3 and initialize it with these values: Jupiter, Saturn, Uranus
// 17. The ArrayLists solarSystem and solarSystem3 should be the same right, use if statements to check with == and .equals()
}
}
I attached the main method for ArrayListPractice and also the instructions. This is due Friday so please help, thank you.
import java.util.ArrayList; // Import the ArrayList class
public class ArrayListPractice {
public static void main(String[] args) {
// 1. Create an ArrayList object that holds Strings, initialize the capacity to
// size 10
ArrayList<String> solarSystem = new ArrayList<String>(10);
// 2. Add a String for each of the first five planets to the solarSystem
// ArrayList
solarSystem.add("Mercury");
solarSystem.add("Venus");
solarSystem.add("Earth");
solarSystem.add("Mars");
solarSystem.add("Jupiter");
// 3. Below is an example of a for-each loop, it will print the entire contents
// of your ArrayList so far
for (String s : solarSystem)
System.out.print(s + ", ");
System.out.println();
// 4. At index 3 insert the String containing the name of our planets only
// natural satellite
solarSystem.add(3, "Moon");
// 5. Use a for-each loop to print the entire contents of your solarSystem
// ArrayList again below
for (String s : solarSystem)
System.out.print(s + ", ");
System.out.println();
// 6. Now add our local star and the collective group of celestial objects
// between Mars and Jupiter at their respective indices
solarSystem.add(3,"Asteroid 3");
solarSystem.add(3,"Asteroid 2");
// 7. The code line below tries to print the element at index 8, why doesn't it
// work, you initialized the capacity to size 10 right?
// Type your answer here then comment out the line below:
//System.out.println(solarSystem.get(8));
// 8. Add the remaining 2 planets that come after Jupiter to your ArrayList, go
// ahead and add Pluto as well
solarSystem.add("Saturn");
solarSystem.add("Uranus");
solarSystem.add("Pluto");
// 9. What is the size of your arraylist now? Print a line that uses the .size()
// method on your ArrayList
System.out.println("size is: "+ solarSystem.size());
// 10. How is the size bigger then the initial capacity you provided? What
// happened?
// Answer:Because ArrayList internally is dynamic array, it will be resize if it reaches its capacity
// 11. Another ArrayList, solarSystem2 that is a "copy" of your first
// solarSystem ArrayList
ArrayList<String> solarSystem2 = solarSystem;
// 12. Print solarSystem2 with a for each loop, is it the same as solarSystem?
// Answer:
for (String s : solarSystem2)
System.out.print(s + ", ");
System.out.println();
// 13. Remove Pluto from the solarSystem ArrayList, it's not really a planet
// anyway right?
solarSystem.remove(solarSystem.size()-1);
// 14. Print solarSystem2, has it changed, why?
for (String s : solarSystem2)
System.out.print(s + ", ");
System.out.println();
// Answer:Because its a deep copy , all the elements are copied with its references
// 15. Remove the first seven elements in the solarSystem ArrayList with a for
// loop, careful you are removing the right indices!
for(int i = 0 ; i<7; ++i) {
solarSystem.remove(0);
}
for (String s : solarSystem)
System.out.print(s + ", ");
System.out.println();
// 16. Make another ArrayList of Strings called solarSystem3 and initialize it
// with these values: Jupiter, Saturn, Uranus
ArrayList<String> solarSystem3 = new ArrayList<String>();
// 2. Add a String for each of the first five planets to the solarSystem
// ArrayList
solarSystem3.add("Jupiter");
solarSystem3.add("Saturn");
solarSystem3.add("Uranus");
System.out.println(solarSystem3==solarSystem);
// 17. The ArrayLists solarSystem and solarSystem3 should be the same right, use
// if statements to check with == and .equals()
System.out.println(solarSystem3.equals(solarSystem));
}
}
=========================
SEE OUTPUT

Overview This lab will continue our exploration of the ArrayList data structure. Specifically, we will cover...