You have ArrayList all wrong,

  • You can't have an integer array and assign a string value.
  • You cannot do a add() method in an array

Rather do this:

List<String> alist = new ArrayList<String>();
alist.add("apple");
alist.add("banana");
alist.add("orange");

String value = alist.get(1); //returns the 2nd item from list, in this case "banana"

Indexing is counted from 0 to N-1 where N is size() of list.

Answer from Buhake Sindi 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
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element ... Returns a shallow copy of this ArrayList instance.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.collections.arraylist.indexof
ArrayList.IndexOf Method (System.Collections) | Microsoft Learn
This method performs a linear search; therefore, this method is an O(n) operation, where n is the number of elements from startIndex to the end of the ArrayList. This method determines equality by calling Object.Equals. This method uses the collection's objects' Equals and CompareTo methods on item to determine whether item exists. ... Searches for the specified Object and returns the zero-based index of the first occurrence within the range of elements in the ArrayList that starts at the specified index and contains the specified number of elements.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
An ArrayList keeps elements in the same order you add them, so the first item you add will be at index 0, the next at index 1, and so on.
🌐
W3Schools
w3schools.com › java › ref_arraylist_indexof.asp
Java ArrayList indexOf() Method
The indexOf() method returns the position of the first occurrence of a value in the list.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › javas-arraylist-indexof-method-explained-6bb47ce4c894
Java’s ArrayList.indexOf() Method Explained | Medium
August 19, 2024 - The indexOf() method is a fundamental part of the ArrayList class, allowing developers to search for elements within the list efficiently. Its primary function is to locate the first occurrence of a given element and return its index.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist.indexof()
Java Arraylist.indexOf() - Get Element Index - HowToDoInJava
January 13, 2023 - Note that string “alex” is present in the list 3 times, but the method returns the index of the first occurrence. Please note that list indices start from 0. ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles","alex",","alex","harry")); int index = list.indexOf("alex"); System.out.println(index);
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › indexOf
Java ArrayList indexOf() - Find Element Index | Vultr Docs
September 27, 2024 - The indexOf() function in Java's ArrayList is a powerful tool for finding the index of elements within the list. Mastery of this function allows you to manipulate lists more efficiently, especially when combined with custom object types or when ...
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraylist_indexof.htm
Java ArrayList indexOf() Method
The Java ArrayList indexOf(Object) method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. This method is used to search an element within arraylist and in case of a
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-indexof-method-example
Java ArrayList indexOf() Method example
In the following example, the element 90 is present twice in ArrayList, at the index 0 and index 3. When we used the indexOf() method to find the index of 90, we got the index of first occurrence of 90, this is because this method returns the index of first occurrence of the specified element.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-arraylist-indexof-java
Java Arraylist indexOf() Method - GeeksforGeeks
December 10, 2024 - The indexOf() method in Java is used to find the index of the first occurrence of a specified element in an ArrayList.
🌐
JavaGoal
javagoal.com › home › how to get index of object in arraylist java
java arraylist indexof and get index of object in arraylist java - JavaGoal
July 26, 2020 - The index(Object o) method is used to get the index of the specified element. Suppose if you have duplicate elements in ArrayList then it returns the index value of first occurrence of the element.
🌐
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?

🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-arraylist-get-method-example
Java ArrayList get() Method example
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.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraylist_get.htm
Java ArrayList get() Method
The Java ArrayList get(int index) method returns the element at the specified position in this list. Index starts from 0 like first element can be retrieved using get(0) method call and so on.
🌐
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 - Well done It would have been easier had the indexOf() method been overloaded to take a starting index. ... You can of course subclass ArrayList and add that method.