Question

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

Overview

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

  • Adding to an ArrayList
  • Adding at a specific index in an ArrayList
  • Looping through an ArrayList with a new kind of loop, the for-each
  • comparing ArrayLists with == and .equals()

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.

  • ArrayListPractice.java

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.

  1. Create an ArrayList object that holds strings, initialize the capacity to size 10.
  2. Add a String for each of the first five planets in the solarSystem ArrayList.
  3. Look at the example of the for-each loop in the provided code, run your code and observe the output.
  4. At index 3 insert the String containing the name of our planets' only natural satellite.
  5. Use a for-each loop like in the provided example to print the contents of the solarSystem ArrayList.
  6. At the appropriate indices insert the name of our local star and the collective group of celestial objects between Mars and Jupiter.
  7. The provided code tries to print the contents of the solarSystem ArrayList at index 8. Run the code and observe that it fails. Why does it fail, didn't you initialize the size of your ArrayList to 10? Answer in the comments of ArrayListPractice.java.
  8. Add the remaining two 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 for the solarSystem ArrayList you provided? Explain what happened in the comments of ArrayListPractice.java.
  11. Observe in the provided code how another ArrayList, solarSystem2 is created as a "copy" of the first solarSystem ArrayList.
  12. Use a for-each loop again to print the contents of solarSystem2 to see if it is the same as the solarSystem ArrayList. Answer whether it is in the comments of ArrayListPractice.java.
  13. Remove Pluto from the solarSyatem ArrayList, it's not really a planet anyway right?
  14. Use the for-each loop once again to print the contents of solarSystem2. It changed, why? Answer in the comments of ArrayListPractice.java.
  15. Remove the first seven elements in the solarSystem ArrayList with a standard for loop. You should still have Saturn, Uranus and Neptune left in the ArrayList when you are done.
  16. Make another ArrayList of Strings called solarSystem3 and initialize it with strings to match the remaining contents of the original solarSystem ArrayList as detailed in the prvious step.
  17. At this point the two ArrayLists solarSystem and SolarSystem3 should be the same. Write if statements to check their equality with the == operator and the .equals() method. Your TA will explain the difference to you in detail
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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
Overview This lab will continue our exploration of the ArrayList data structure. Specifically, we will cover...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT