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

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
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
Java: Is Arraylist better than Arrays?
The biggest difference is that you can add to and remove from a list, whereas arrays have a fixed size. More on reddit.com
🌐 r/learnprogramming
28
19
June 23, 2024
Two dimensional ArrayList, need help with constructor
There are probably better ways to solve your problem, but here is the answer to your question: List> list1 = new ArrayList>(); for (int i = 0; i < 10; i++) { List list2 = new ArrayList(); for (int j = 0; j < 10; j++) { list2.add(i * j); } list1.add(list2); } Integer value = list1.get(1).get(5); More on reddit.com
🌐 r/java
5
0
February 17, 2012
🌐
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?
🌐
TutorialsPoint
tutorialspoint.com › get-the-index-of-a-particular-element-in-an-arraylist-in-java
Java ArrayList indexOf() Method
September 13, 2023 - We're adding couple of Integers to the ArrayList object using add() method calls per element and using indexOf(object) method, we're checking index of an element and printing it. package com.tutorialspoint; import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // create an empty array list ArrayList<Integer> arrayList = new ArrayList<>(); // use add() method to add elements in the arrayList arrayList.add(0); arrayList.add(1); arrayList.add(2); arrayList.add(3); arrayList.add(4); arrayList.add(5); arrayList.add(6); // let us print index of 5 System.out.println("Index of 5 = " + arrayList.indexOf(5)); } }
🌐
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"); ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-util-arraylist-indexof-java
Java Arraylist indexOf() Method - GeeksforGeeks
December 10, 2024 - Example 1: Here, we will use the indexOf() method to find the index of a specific element in an ArrayList. ... // Java program to demonstrate the // use of indexOf() method import java.util.ArrayList; public class GFG { public static void ...
🌐
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 - To get the index of the last occurrence of the same element, use the lastIndexOf() method. The indexOf() returns the index of the first occurrence of the specified element in this list. It will return '-1' if the list does not contain the element.
Find elsewhere
🌐
W3Resource
w3resource.com › java-tutorial › arraylist › arraylist_indexof.php
Java ArrayList.indexOf() Method
August 19, 2022 - Return Value: The index of the first occurrence an element in ArrayList, or -1 if the element does not exist. ... The following example creates an ArrayList containing various colors. It then tries to determine the position of a certain color. import java.util.*; public class test { public ...
🌐
Programiz
programiz.com › java-programming › library › arraylist › indexof
Java ArrayList indexOf()
returns the position of the specified element from the arraylist · Note: If the specified element doesn't exist in the list, the indexOf() method returns -1. import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<Integer> numbers ...
🌐
Medium
medium.com › @AlexanderObregon › javas-arraylist-indexof-method-explained-6bb47ce4c894
Java’s ArrayList.indexOf() Method Explained | Medium
August 19, 2024 - This method accepts a single argument, Object o, which represents the element you wish to find within the ArrayList. If the specified element is present in the list, indexOf() returns the zero-based index of its first occurrence.
🌐
W3Schools
w3schools.com › java › ref_arraylist_indexof.asp
Java ArrayList indexOf() Method
public int indexOf(Object item) Java Arrays Tutorial · Java ArrayList Tutorial · ❮ ArrayList Methods · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US · × · If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ·
🌐
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. add(index, value) method is used to insert the ...
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › arraylist get() – get element at index
Java ArrayList - Get Element at Index
January 12, 2023 - For example, if ArrayList holds 10 objects, then a valid argument index will be between 0 to 9 (both inclusive). The get() method returns the reference of the object 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.
🌐
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.
🌐
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.
🌐
Codekru
codekru.com › home › arraylist indexof() method in java
ArrayList indexOf() method in Java - Codekru
August 12, 2022 - What does it do? It will return the index of the first occurrence of the specified element. So, if an element is present multiple times in an ArrayList, then indexOf() will return the index of the first occurrence of that element in the list.
🌐
W3Schools
w3schools.com › java › java_arraylist.asp
Java ArrayList
It is part of the java.util package and implements the List interface. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one).
🌐
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?