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
How to get current index of ArrayList?
What do you mean by "current" index? You get an any arbitrary index by using the get(int) method of ArrayList. More on reddit.com
🌐 r/javahelp
6
3
October 13, 2015
Why is .remove(index) of ArrayList not working?
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
25
9
March 23, 2021
Java Array list with more than 2^31 items?
Is it possible to create an array list in Java that holds more than 231 Items? In a single java.util.ArrayList? No. Java array indices are integer based and you won't be able to create a single array longer than that. However you could implement your own version of an arraylist that used a long index and used the first 4 bytes of a long to index into a list containing the underlying arrays. So can you create "an array list that can hold more than 231 items": sure. Can a standard java.util.ArrayList hold more? No. More on reddit.com
🌐 r/learnprogramming
8
1
October 30, 2014
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
October 20, 2025 - 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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 ...
🌐
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.
🌐
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.
🌐
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?

🌐
Learneroo
learneroo.com › modules › 26 › nodes › 192
More About ArrayList - Learneroo
With an ArrayList, you can just use the provided method indexOf. It will search through the list and return the index of the first occurrence of the Object. If the Object isn't there.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 7 )
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.
🌐
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.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java ArrayList Insert/Replace At Index - Java Code Geeks
January 2, 2022 - ArrayList.add(int index, E element) method takes the index of the array list where the new value to be inserted and E element that the new value to be added to the existing values of ArrayList.
🌐
Programiz
programiz.com › java-programming › library › arraylist › indexof
Java ArrayList indexOf()
In the above example, we have created an arraylist named numbers. Notice the expressions, // returns 1 numbers.indexOf(13) // returns -1 numbers.indexOf(50)
🌐
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 › ref_arraylist_indexof.asp
Java ArrayList indexOf() Method
The indexOf() method returns the position of the first occurrence of a value in the list.
🌐
TutorialsPoint
tutorialspoint.com › get-the-index-of-a-particular-element-in-an-arraylist-in-java
Get the index of a particular element in an ArrayList in Java
September 13, 2023 - The index of a particular element in an ArrayList can be obtained by using the method java.util.ArrayList.indexOf(). This method returns the index of the first occurrence of the element that is specified. If the element is not available in the ArrayList, then this method returns -1.