FAQ: Learn Java: ArrayLists - Removing an Item

This community-built FAQ covers the “Removing an Item” exercise from the lesson “Learn Java: ArrayLists”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Removing an Item

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

My solution for step 7/9:

// Remove each to-do below:
    // Remove "visit the crime scene" from 
    // sherlocksToDos and poirotsToDos using remove().
    
    sherlocksToDos.remove("visit the crime scene");
    poirotsToDos.remove("visit the crime scene");
    
    // remove "play violin" from sherlocksToDos 
    sherlocksToDos.remove("play violin");
1 Like

Due to the many advantages of the ArrayList over a simple array, are there cases at all in what i should prefer an array over an arrayList?

Thx for your input and support.

4 Likes

Is it possible to remove two items from Sherlock’s with one line of code?
I tried as below

sherlocksToDos.remove(0, 1);

and it gave me :

ToDos.java:27: error: no suitable method found for remove(int,int)
sherlocksToDos.remove(0, 1);
^
method Collection.remove(Object) is not applicable
(actual and formal argument lists differ in length)
method List.remove(Object) is not applicable
(actual and formal argument lists differ in length)
method List.remove(int) is not applicable
(actual and formal argument lists differ in length)
method AbstractCollection.remove(Object) is not applicable
(actual and formal argument lists differ in length)
method AbstractList.remove(int) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.remove(int) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.remove(Object) is not applicable
(actual and formal argument lists differ in length)
1 error

I wanted to try both solutions so below is how I wrote my code :

    sherlocksToDos.remove(0);
    sherlocksToDos.remove(0);
    poirotsToDos.remove("visit the crime scene");

it wouldn’t allow me to write as below, as (I guess) I’ve already removed the 0 index with the first line of code :
sherlocksToDos.remove(0);
sherlocksToDos.remove(1);
poirotsToDos.remove(“visit the crime scene”);

possible, have you tried to use google to find the information you require?

what you want to do is not possible using remove, if you look at the documentation, you can verify this

Thanks for the reply and also for reminding me about google.
I got used to checking the Forum.

Found that with remove(), I can only remove one item, and to remove multiple, I can use removeAll().

Thanks again.

3 Likes

So you can remove by specifying the index, or the value, to remove. That’s great and all, but the first thing that pops into my mind is, what if it is an array list of integers?

ArrayList example = new ArrayList();

example.add(4);
example.add(12);
example.add(0);
example.add(1);

example.remove(1);

Will it remove the element at index 1, leaving me with 4, 0, 1? Or will it remove the element with the value 1, leaving me with 4, 12, 0? And whichever one of those ends up being the case, how do I make it work the other way if I need to?

2 Likes

Given you already have the code, would be simple to find out? Simple print the array list to see which value/index got removed. Or consult the documentation on the remove method (which very likely include the working of the method)

1 Like

What if I declare an ArrayList of Integers, When I use .remove by specifying the value to remove my question is how Java differentiate between is it an index or is it a value remove?

2 Likes

It says we can remove using the index # or by specifying the value.

What would be the case if we were removing items from a integer array list. Would it assume that I want to remove a certain number or that I am referring to the index number, as they are both integers.

2 Likes

import java.util.ArrayList;

class ToDos {

public static void main(String args) {

// Sherlock
ArrayList<String> sherlocksToDos = new ArrayList<String>();

sherlocksToDos.add("visit the crime scene");
sherlocksToDos.add("play violin");
sherlocksToDos.add("interview suspects");
sherlocksToDos.add("solve the case");
sherlocksToDos.add("apprehend the criminal");

// Poirot
ArrayList<String> poirotsToDos = new ArrayList<String>();

poirotsToDos.add("visit the crime scene");
poirotsToDos.add("interview suspects");
poirotsToDos.add("let the little grey cells do their work");
poirotsToDos.add("trim mustache");
poirotsToDos.add("call all suspects together");
poirotsToDos.add("reveal the truth of the crime");

// Remove each to-do below:
sherlocksToDos.remove(1);
poirotsToDos.remove(0);
sherlocksToDos.remove(0);

 
  
System.out.println(sherlocksToDos.toString() + "\n");
System.out.println(poirotsToDos.toString());

}

}
** It’s possible to remove members from the Array-list with their sequence number, but then you need to start removing from the highest number. If you remove the member 0, the last member in the position 1 transfer to the position 0, which changes the whole sequence.**

1 Like

I know this is now a year old, but at this level I often find it quite difficult to find answers to basic coding questions using Google. In most cases the results that come up might contain the answer, but often in scenarios complicated enough that it’s hard for a relative beginner to be sure of the meaning.

Also, your question was exactly the same as my question, so I’m glad you asked it. I thought that was the whole point of this forum anyway.

1 Like

I am currently reviewing all the lesson that I took before, and found out that I used to print ArrayList by adding .toString() method. Like the code below.

image

Problem is, both printed result was same.
So I want to know what actually happens when I add the .toString() method.

1 Like