Store an already "reversed" list into a new list (JAVA)

I just got out of a 1-week internship with my company and I was still left with a burning (yet not resolved) question.

  1. I have this working code.
import java.util.*;

public class Tiergarten {

	public static void main(String[] args){

		
		List<String> myList1 = Arrays.asList("affe", "vogel", "giraffe", "katze");
		int reverseArrayCounter = myList1.size() -1;

		for (int i = reverseArrayCounter; i>= 0; i--){
			System.out.println(myList1.get(i));
		}
		
   }

}

This List prints out the following sequence “katze, giraffe, vogel, affe”. So I was able to reverse this list.

  1. Now into a problem - I want to save this reversed list value into a new ArrayList or similar and then I want to keep adding/removing elements from the list.

I know how to add or remove stuff on the list but I am totally stuck at saving the values (the reversed list) in a new (Array) List. I have tried ArrayList<Double>, HashMap ‘copy’ etc but none of these work out for me. Could anyone give me a hint?

Learning to read the documentation is your best friend in situations like this. To find the documentation you can simply search “ArrayList Java” and find the Oracle webpage somewhere in the top of your search results. Here is the ArrayList documentation.

I’ve studied Java for a year and I definitely don’t understand everything in the documentation, but let’s not get intimidated by that. In this case we only care about understanding one or a few specific things about the ArrayList class.

Using a constructor seems to be a good idea to create an empty ArrayList. If you are unsure about what the constructor looks like there is a list in the documentation. This one seems like a good choice:

ArrayList()
Constructs an empty list with an initial capacity of ten.

[Quote 1]

We can also find a list of available methods. In this case we can find a suitable method at the top:

add(E e)
Appends the specified element to the end of this list.

[Quote 2]

Each method tells us what parameters are required and what type the parameters should have. If we hover the “E” you get a small explaination of what “E” means: “Type parameter in ArrayList.”

Also note the name of the class:

public class ArrayList<E>

[Quote 3]

Sometimes it’s helpful to Google unfamiliar technical terms; sometimes the syntax is familiar from code you’ve already written. In this case, you’ve might have noticed that you used this syntax before:

List<String> myList1 = Arrays.asList(“affe”, “vogel”, “giraffe”, “katze”);

[Quote 4]

List has a type parameter that defines what type of object can fit inside that list, in your case String. If we look back at the method documentation (quote 2) we now know that the method we want to use requires that we provide an element of the same type as the element defined in the angled brackets.

You wrote that you’ve tried “ArrayList<Double>”, but that means you cannot use the add method we found in the documentation. At least not if we want to add elements of the type String, as in your example. Instead, let’s create a list that fits our needs:

List<String> reversedList = ArrayList<String>();

Now we have an empty list that we can add elements of the type String to.

Let me know if you need more help with your problem!

As a final note, reading the documentation is a good tool, but I also recommend using your IDE. I use IntelliJ. The tools you get in a good IDE can often provide help: Example with images

Hi @worreu thank you so much for the solution and all the explanation! I appreciate you for explaining why I can’t do “ArrayList” when I have “List”.

I also completely forgot about “reverseList” - I think I misunderstood the task as well.

Thank you so much for helping me clear my head :blush:

1 Like

No worries!

Note: I made a mistake in my answer…

This is the correct way to make a list:

List<String> reversedList = new ArrayList<>()

The mistake that I wrote is a bad way to make a list, but it actually works:

List reversedList = new ArrayList()

Why does it work? I think it’s the same as writing:

List<Object> reversedList = new ArrayList<>()

We essentially created a list that can contain any Object, a.k.a anything. This might seem convenient, but it’s a bad habbit. Let’s add two different type of objects to our list:

Double someNumber = 5.0;
Double someOtherNumber = 2.0;
String someText = “hello”;
reversedList.add(someText);
reversedList.add(someNumber);
reversedList.add(someOtherNumber);

Note that we added a String and two Doubles to our list. The code is still working, so this might not always cause issues, but imagine you created your list to contain some numbers that you want to do calculations with. Let’s say we want to add all numbers together.

When you add 5.0 and 2.0 together it becomes 7.0. When you add 7.0 and “hello” together it becomes “7.0hello”. This can mean that your code will run, but if you give the wrong input it will crash.

With that cleared out, I just want to clear up some possible misconceptions:

  1. It is possible to do: List<String> myList1 = new ArrayList<String>() or List<String> myList2 = new ArrayList<>(). These two mean the same thing.

  2. It is not possible to do List<String> impossibleList = ArrayList<Double>(). This is similar to: String exampleVariable = 1.0. You can’t declare a variable and then give it a value that it cannot contain.

I think the first two points explains your original question.

  1. It is impossible to do List<String> impossibleList = List<>(). Why? If you look in the List documentation you see that List is an interface – not a class. If you’re not familiar with interfaces, think of it like requirements of what methods should exist. However, the interface is just an empty shell and the required methods are not implemented.

  2. So why is it possible to do the following?

List<String> someKindOfList = new ArrayList<>();

Since there are two things happening here, let’s make them clear:

List<String> someKindOfList;
someKindOfList = new ArrayList<>();

On the first line, we create a variable and decide that it should be some kind of List.
On the second line, we give the variable a value. In this case we want an ArrayList, but it’s not our only choice. You can see a list of all classes in the List documentation.

  1. So why does the following not work?

List<String> someKindOfList;
someKindOfList = new HashMap<>();

If you look in the HashMap documentation, you see that the HashMap class does not implement the List interface (at the top, “all implemented interfaces”).

When you declare a variable you can be broad and say it’s going to be “some type of list” (interface).
When you initialize a variable (give it a value), you have to provide a “specific type of list” (class).