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
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 )
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.
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
I need help to understand how Arraylist works in java
Arraylist.add(x) More on reddit.com
🌐 r/learnprogramming
7
2
March 26, 2025
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
CodeHS
codehs.com › tutorial › 13901
Tutorial: ArrayLists in Java | CodeHS
Learn how to create and use ArrayLists in your programs!
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-in-java
ArrayList in Java - GeeksforGeeks
Automatic Resizing: When the internal array becomes full, the ArrayList automatically increases its capacity by creating a new larger array and copying the existing elements into it. Index-Based Access: Elements are stored in contiguous memory locations, allowing fast access using indexes (e.g., get(index)), typically in O(1) time complexity.
Published   May 12, 2026
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › dsa-in-java
DSA in JAVA - GeeksforGeeks
October 8, 2025 - This beginner-friendly guide covers Data Structures and Algorithms (DSA) in Java, including built-in structures like arrays, strings, ArrayList, HashMap, HashSet, and user-defined structures such as linked lists, stacks, queues, trees, heaps, and graphs.
🌐
W3Schools
w3schools.com › java › java_list.asp
W3Schools.com
You can access elements by their index, add duplicates, and maintain the insertion order. Since List is an interface, you cannot create a List object directly. Instead, you use a class that implements the List interface, such as: ArrayList - like a resizable array with fast random access
🌐
CODEDOST
codedost.com › home › java › collections in java › get(int index) method of arraylist in java
get(int index) method of ArrayList in JAVA | CODEDOST
July 25, 2021 - In order to retrieve an element from an arrayList, get(int index) method is used. get(int index) method returns the value at the specified index and if index is outside the range, it will throw an IndexOutOfBounds Exception.
🌐
Simon Hartcher
simonhartcher.com › posts › 2026-04-08-applying-programming-without-pointers-to-an-mbox-indexer-using-zig
Applying "Programming Without Pointers" to an mbox indexer using Zig | Simon Hartcher
April 8, 2026 - I find them fun to write and very easy to comprehend. Due to the simple data structures of the index, writing takes zero allocations. And because of the file design and PWP, reading only requires two! One for the message_ids array list, and one for the locations hash map.
🌐
DEV Community
dev.to › riansyah › javabite-arraylist-and-linkedlist-367b
ArrayList and LinkedList in Java - DEV Community
December 29, 2024 - ArrayList An ArrayList is like a dynamic array. It stores elements by their index, so you can quickly jump to any element you want.
🌐
Baeldung
baeldung.com › home › java › java list › inserting an object in an arraylist at a specific position
Inserting an Object in an ArrayList at a Specific Position | Baeldung
April 3, 2025 - If we want to add an element to a specific position to an ArrayList we can use the add(int index, E element) method which is provided through the implementation of the interface of List<E>. This method let us add an element at a specific 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);
🌐
LeetCode
leetcode.com › problems › two-sum-ii-input-array-is-sorted
Two Sum II - Input Array Is Sorted - LeetCode
7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2.
🌐
Microsoft Learn
learn.microsoft.com › ru-ru › dotnet › api › system.collections.arraylist.indexof
ArrayList.IndexOf Метод (System.Collections) | Microsoft Learn
Выполняет поиск указанного Object и возвращает отсчитываемый от нуля индекс первого вхождения в течение всего ArrayList. public: virtual int IndexOf(System::Object ^ value);