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
🌐
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).
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › CMSC150 › 062ArrayLists › ArrayLists.html
Working with ArrayLists
ArrayList<Integer> D = new ArrayList<Integer>(); D.add(3); // Location 0 is 3 D.add(15); D.add(-46); D.set(0,22); // Location 0 is now 22 · Along with being to add items and have the list resize automatically to accomodate them, you can also remove items and have the list shrink automatically each time you remove an item. To remove an item, use the remove() method with the index of the item you want to remove.
Discussions

Java ArrayList Index - Stack Overflow
Say that I want to use the second item in the ArrayList. More on stackoverflow.com
🌐 stackoverflow.com
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
[Java] Trouble adding an object to an Arraylist
You have an infinite loop occurring. When you see a name match, you insert a new student record into that index. This shifts the record you matched one to the right. When the ArrayList moves onto the next index, you see the previous record again and again insert, causing an infinite loop. Or to make it more clear, given a list: [Alice][Bob][John] and a new student Bob: [Alice][Bob][John] //Find a match [Alice][Bob][Bob][John] //Insert a new Bob into Index 1, prev Bob becomes Index 2 [Alice][Bob][Bob][John] //Finds a match with the shifted record [Alice][Bob][Bob][Bob][John] //Insert a new Bob into Index 2 Repeat until you run out of memory More on reddit.com
🌐 r/learnprogramming
7
1
May 14, 2014
Finding max index of ArrayList (No Collections)
If you're looking to return the max index then other answers have you covered. If you need the index of the max value, then you need two max variables. One to hold the current max value, and one to hold the index of that max value More on reddit.com
🌐 r/javahelp
16
10
November 27, 2018
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-in-java
ArrayList in Java - GeeksforGeeks
ArrayList in Java is a resizable array provided in the java.util package. Unlike normal arrays, its size can grow or shrink dynamically as elements are added or removed. Elements can be accessed using their index, just like arrays.
Published   1 month ago
🌐
Coderanch
coderanch.com › t › 642278 › java › ArrayList-index-check
ArrayList index check (Beginning Java forum at Coderanch)
November 12, 2014 - If you have an array or a list of length N, then any index between 0 and N - 1 (inclusive) is a valid index for the array or list. The indexOf method of interface List is something else. It checks if there is an object in the list that is equal to the object you pass to the method, and if it ...
🌐
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 › home › java/util › java arraylist add() method with index
Java ArrayList add() Method with Index
September 1, 2008 - Discover how to effectively use the add() method in Java's ArrayList to insert elements at specific indices with practical examples.
🌐
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)
🌐
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.
🌐
Reddit
reddit.com › r/javahelp › how to get current index of arraylist?
r/javahelp on Reddit: How to get current index of ArrayList?
October 13, 2015 -

So, I have an assignment to create an address book with ArrayList. The program uses "back" and "next" buttons to navigate through names. In order to make the buttons work, I feel like I should first find the current index, then increment or decrement from there. So what is the best way to get the current index of an ArrayList?

🌐
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"); ...
🌐
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.
🌐
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 - ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles","alex",","alex","harry")); int index = list.indexOf("alex"); System.out.println(index); Program output. ... We can use this method to find if an object is present in the arraylist. If the object is present, then the return value will be greater than '-1‘. Happy Learning !! ... A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
1 week ago - JavaScript arrays are not associative arrays and so, array elements cannot be accessed using arbitrary strings as indexes, but must be accessed using nonnegative integers (or their respective string form) as indexes.
🌐
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 ·