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.
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.
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
I need help to understand how Arraylist works in java
Arraylist.add(x) More on reddit.com
Videos
17:26
ArrayList / Lists - Full Tutorial - Java Data Structures Tutorial ...
09:40
Learn Java arraylists in 9 minutes! 📃 - YouTube
01:37
#121 Java ArrayList indexOf() – Find Element Position with Examples ...
04:03
Does ArrayList has indexing in Java (Core Java Interview Question ...
Traversing a Java ArrayList (Printing an ArrayList) - YouTube
Top answer 1 of 7
110
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.
2 of 7
20
Read more about Array and ArrayList
List<String> aList = new ArrayList<String>();
aList.add("apple");
aList.add("banana");
aList.add("orange");
String result = alist.get(1); //this will retrieve banana
Note: Index starts from 0 i.e. Zero
- Resource
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.
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.
Reddit
reddit.com › r/javahelp › a better way to find the index of an element in an array?
r/javahelp on Reddit: A better way to find the index of an element in an array?
August 27, 2022 -
Currently the best way I have found is to typecast the array into an ArrayList, and using ArrayList's method .indexOf to get the index, for e.g:
import java.util.Arrays;
int[] arr = {2,3,4,5};
Arrays.asList(arr).indexOf(4); // returns 2Is this the best way? Or unnecessary wrapping?
Top answer 1 of 4
7
That way doesn't work. int[] can't be wrapped up as a List via List Arrays.asList(T...) - you'll get a List back (with a single element). If the array is sorted you can use Arrays.binarySearch(arr, 4) - if it is not sorted then you've got fewer choices. Most efficient is probably... int index = -1; for (int i = 0; i < arr.length; i++) { if (arr[i] == x) { index = i; break; } } But you could also do int index = IntStream.range(0, arr.length) .filter(i -> arr[i] == x) .findFirst() .orElse(-1); If the array was spectacularly large, this might pay off in parallel (but the findFirst limits this as it forces some sequencing to the parallel operation - findAny would help). Generally I'd just make a method to do the for-loop (or try to avoid needing to scan in the first place - not always possible).
2 of 4
3
If you're doing this, why not just use Lists instead of arrays?
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.
Reddit
reddit.com › r/learnprogramming › i need help to understand how arraylist works in java
r/learnprogramming on Reddit: I need help to understand how Arraylist works in java
March 26, 2025 -
Example I have made an arraylist of 3 elements.(0,1,2 indexes)
I want to add another element in the 4th index, will java automatically fill the empty indexes or do I have to run a while loop in order to fill those empty spaces?
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);