Since [] is an operator and java does not support operator overloading you can't use it with List. Instead you have to use the set(int index, T value) and get(int index) methods, which may be verbose but provide exact the same functionality.

Answer from josefx on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ arraylist-get-method-java-examples
ArrayList get(index) Method in Java with Examples - GeeksforGeeks
December 10, 2024 - The get(index) method of ArrayList in Java is used to retrieve the element at the specified index within the list.
Discussions

Java while loop to find index of elements in ArrayList
ArrayList.indexOf(Object o) will always return the index of the first occurrence of the given object, so what you observe is happening by design; to get the indices of the others, you would have to remove the item from the list once you find it so that when you call indexOf again, the next occurrence is seen. Alternatively, you could scan through the list and compare elements directly instead of using indexOf. That way, you wouldn't have to modify the list at all, since you'd simply be looking at each item and comparing with a reference value. More on reddit.com
๐ŸŒ r/learnprogramming
13
1
May 4, 2017
A better way to find the index of an element in an array?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
23
3
August 27, 2022
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ List.html
List (Java Platform SE 8 )
October 20, 2025 - The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example).
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_arraylist.asp
Java ArrayList
Now you can use methods like add(), get(), set(), and remove() to manage your list of elements. To add elements to an ArrayList, use the add() method: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); System.out.println(cars); } } Try it Yourself ยป ยท You can also add an element at a specified position by referring to the index number: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add(0, "Mazda"); // Insert element at the beginning of the list (0) System.out.println(cars); } } Try it Yourself ยป ยท
๐ŸŒ
Jenkov
jenkov.com โ€บ tutorials โ€บ java-collections โ€บ list.html
Java List
February 29, 2020 - You can find elements in a Java List using one of these two methods: ... The indexOf() method finds the index of the first occurrence in the List of the given element.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_arraylist_indexof.asp
Java ArrayList indexOf() Method
Find the position of an item in a list: import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); System.out.println(cars.indexOf("Ford")); } } Try it Yourself ยป ยท
Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ arraylist โ€บ .indexof()
Java | ArrayList | .indexOf() | Codecademy
February 12, 2023 - The .indexOf() method returns the index of the first occurrence of the specified element in an ArrayList. If the element is not found, -1 is returned. ... Looking for an introduction to the theory behind programming?
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 622130 โ€บ java โ€บ ArrayList-Find-Indexes-Occurrence
ArrayList: Find all Indexes of an Occurrence [Solved] (Beginning Java forum at Coderanch)
October 19, 2013 - If you have (and once you've completed the task above), make sure you have a copy of the API docs for java.util.ArrayList (โ†click) open on your desktop when you start coding. (BIG Hint: Once you HAVE read the API docs, you'll probably discover that you can do what you want in 1 line of code - well, maybe a few more to get ALL indexes ) HIH Winston
๐ŸŒ
Medium
medium.com โ€บ @AlexanderObregon โ€บ javas-arraylist-indexof-method-explained-6bb47ce4c894
Javaโ€™s ArrayList.indexOf() Method Explained | Medium
August 19, 2024 - The indexOf() method in Java's ArrayList class is a straightforward yet powerful tool for locating the first occurrence of an element within a list. Understanding its internal workings, performance considerations, and practical applications ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ java while loop to find index of elements in arraylist
r/learnprogramming on Reddit: Java while loop to find index of elements in ArrayList
May 4, 2017 -

I'm sorry for the possibly bad title! Didn't know what else to put there.

So my problem is, I am doing the MOOC course for java from University of Helsinki, and my challenge was to create a program which reads input from user and puts it on an ArrayList. If the user enters -1, the reading will end. After the list has been created (user enters -1), the program will ask the user for number and print out all the indexes where that number exists in the ArrayList. I have blasted my way through all of the earlier challenges but just can't manage to wrap my head around this. I do know how to solve this with for-loop and arrays, but I'm supposed to solve it using a while loop and without the use of arrays.

My current code is this:

if(list.contains(number)) {
    while(i < list.size()) {
        spot = list.indexOf(number);
        i++;
    }
    System.out.println("Number " + number + " is in the index " + spot);
} 

and it works just fine when I have a list that contains only 1 instance of a number. But when I have a list that has for example two times the number eight in different indexes, it will only return the first one. Any suggestions?

๐ŸŒ
Medium
medium.com โ€บ javarevisited โ€บ i-need-an-index-with-this-list-iteration-method-1e339fd55ed7
I need an index with this List iteration method | by Donald Raab | Javarevisited | Medium
February 10, 2024 - In Java, there are a few ways to to iterate over a List with indices. I will cover a few of the most common external iteration approaches, and how they can be combined with providing an index.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-do-you-get-the-index-of-an-element-in-a-list-in-java
How do you get the index of an element in a list in Java?
June 5, 2025 - Then we will use the indexOf() method to find the index of a specific value in the List. import java.util.ArrayList; import java.util.List; public class GetIndexOfElement { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2013 โ€บ 12 โ€บ java-arraylist-get-method-example
Java ArrayList get() Method example
September 11, 2022 - ArrayList get(int index) method is used for fetching an element from the list. We need to specify the index and get method returns the value present at the specified index.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-util-arraylist-indexof-java
Java Arraylist indexOf() Method - GeeksforGeeks
December 10, 2024 - Example 2: Here, we use the indexOf() method to find the first occurrence and lastIndexOf() to find the last occurrence of a specific element. ... // Java program to demonstrate the use of // indexOf() and lastIndexOf() methods import ...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ searching and changing the index of an element in a list of objects, how can i do this more elegantly in java?
r/learnprogramming on Reddit: Searching and changing the index of an element in a list of objects, how can I do this more elegantly in Java?
October 7, 2021 -

I have a list of objects stored in ArrayList. I want to find one element in this list based on a condition and shift it to the beginning of list. Here's how I'm dong it right now:

reOrderList(List<Room> roomList, int personId) {
    for(Room room : roomList) {
        boolean found = false;
        for (Person person : room.getPersonList()) {
            if (person.getId() == personId) {
                found = true;
                break;
            }
        }
        if (found) {
            Room foundRoom = room;
            list.remove(room);
            list.add(0, foundRoom);
            break;
        }
    }
}

The room and person class look like this:

class Room {
    List<Person> personList;
}
class Person {
    int Id;
}

As seen from the code, I have a person's id, I want to check which room in the list contains that person and then shift that room to the start of room's list.

I have done a very basic implementation above, just want to know if there can be more refined way of doing this using any in-build features of Java? I meant can I somehow use stream or forEach, I used for loops because I needed break statement as there will be only one element to be found. And is there any other way of changing index of element to the beginning rather than removing and appending at the starting?

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ home โ€บ java/util โ€บ java arraylist get() method
Java ArrayList get() Method
September 1, 2008 - The Java ArrayList get(int index) method returns the element at the specified position in this list.
๐ŸŒ
Runestone Academy
runestone.academy โ€บ ns โ€บ books โ€บ published โ€บ apcsareview โ€บ ListBasics โ€บ listMethods.html
9.12. Getting and Setting Values in a List โ€” AP CSA Java Review - Obsolete
The add with an index of 2 and a value of 5 adds the 5 at index 2 not 1. Remember that the first index is 0. You can step through the code above by clicking on the following Example-8-6-1. 8-6-4: What will print when the following code executes? List<String> list1 = new ArrayList<String>(); list1.add("Anaya"); list1.add("Layla"); list1.add("Sharrie"); list1.set(1, "Destini"); list1.add(1, "Sarah"); System.out.println(list1);
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ indexOf
Java ArrayList indexOf() - Find Element Index | Vultr Docs
September 27, 2024 - Create an instance of ArrayList and add some elements to it. Use the indexOf() method to find the index of a specified element. ... import java.util.ArrayList; ArrayList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); ...
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-list
Java List - List in Java | DigitalOcean
August 3, 2022 - Java List is an ordered collection. Java List is an interface that extends Collection interface. Java List provides control over the position where you can insert an element. You can access elements by their index and also search elements in the list.