ArrayList<Matrices> list = new ArrayList<Matrices>();
list.add( new Matrices(1,1,10) );
list.add( new Matrices(1,2,20) );
Answer from Aaron Saunders 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). While elements can be added and removed from an ArrayList whenever you want. To use an ArrayList, you must first import it from java.util: Create an ArrayList object called cars that will store strings:
Discussions

Learning Java but stuck on Array lists using objects
public class MyCustomObject { private int id; private String name; public MyCustomObject (int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } } MyCustomObject mco1 = new MyCustomObject(1,"foo"); MyCustomObject mco2 = new MyCustomObject(2,"bar"); ArrayList listOfCustomObjects = new ArrayList<>(); listOfCustomObjects.add(mco1); listOfCustomObjects.add(mco2); listOfCustomObjects.get(0).setId(3); String secondsName = listOfCustomObjects.get(1).getName(); You can easily access methods of object present in the List, by using their index for example: list.get(int index). Another way would be to iterate through all the objects and performing some action. Keep in mind all the elements of the list should be of the same type or extend the same type. It is not recommended but if you really want to you can create ArrayList that can pretty much hold all different types, Custom objects, Integers, Strings. More on reddit.com
🌐 r/javahelp
9
9
June 2, 2022
Creating an array list of objects.
What is the error/problem? You absolutely have to include the error message verbatim, as the compiler/runtime gives. Luckily, the problem is easy to spot: you are trying to call a method resourceList.add(log) outside of any method. This does not work in Java. You can only declare variables and initialize them, but not perform other operations outside methods. You will need to add the above method call into some method, e.g. into public static void main (at least for testing). Commonly, you would have a method addResource that takes in a Resource instance (object of type Resource) which does exactly what you do in the above, quoted line - adds the resource to the ArrayList. Alternatively, and I wouldn't recommend it without looking deeper into it, you could use a static initializer block right after your public static ArrayList... declaration. Edit: Sidenote: be wary of making everything static. Don't even get into that habit. Not even at the beginning. Try to avoid it by default. There is nothing wrong with making things non-static and instantiating even a single object of that class. Getting into the habit of not using static will help you extremely much in the long run and will make your OOP programming much easier and better (even though in the beginning it seems to be the opposite). More on reddit.com
🌐 r/javahelp
7
1
December 5, 2024
How do you add array of objects into an Array List?
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
7
2
March 18, 2022
Is using ArrayList good practice for a regular software engineering job?
How often do software engineers use ArrayList? Always. I write readable, maintainable code. I'm not "l33t h4xor", I'm fucking working. More on reddit.com
🌐 r/java
232
126
August 10, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-create-array-of-objects-in-java
How to Create Array of Objects in Java? - GeeksforGeeks
Explanation: The for loop iterates over each Student object in the "s" array. Inside the loop, s1.display() is called to print the details of each Student object. ... In Java, we can create an array of objects just like any other array.
Published   July 15, 2025
🌐
Javatpoint
javatpoint.com › java-arraylist
Java ArrayList - javatpoint
June 14, 2013 - Java ArrayList in Java collections with add, example of generic collection vs non-generic, addAll, remove, removeAll, contains, containsAll, retainAll, clear and iterator methods, generic and non-generic collection.
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist: a comprehensive guide for beginners
Java ArrayList: A Comprehensive Guide for Beginners
November 6, 2023 - The Java ArrayList represents a resizable array of objects which allows us to add, remove, find, sort and replace elements. The ArrayList is part of the Collection framework and implements in the List interface.
🌐
Scaler
scaler.com › home › topics › array of objects in java
Array of Objects in Java - Scaler Topics
January 11, 2024 - Java emphasizes object-oriented programming, relying heavily on classes and objects. While a single object can be managed using an Object variable, multiple objects are efficiently handled using an Array of Objects. Unlike typical arrays that store primitive types like integers or booleans, an Array of Objects accommodates object references.
🌐
Reddit
reddit.com › r/javahelp › learning java but stuck on array lists using objects
r/javahelp on Reddit: Learning Java but stuck on Array lists using objects
June 2, 2022 -

Hi all, so as the title says I'm learning java and using Udemy to do so, I've hit a topic where arraylists are used with a custom object.

I don't think the course fully explains the concept of arraylists or how the qualities of the objects / reference to the objects can be / should be used.

If anyone has a link to a post or video going into detail on how to use arraylists with objects please let me know.

Top answer
1 of 4
5
public class MyCustomObject { private int id; private String name; public MyCustomObject (int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } } MyCustomObject mco1 = new MyCustomObject(1,"foo"); MyCustomObject mco2 = new MyCustomObject(2,"bar"); ArrayList listOfCustomObjects = new ArrayList<>(); listOfCustomObjects.add(mco1); listOfCustomObjects.add(mco2); listOfCustomObjects.get(0).setId(3); String secondsName = listOfCustomObjects.get(1).getName(); You can easily access methods of object present in the List, by using their index for example: list.get(int index). Another way would be to iterate through all the objects and performing some action. Keep in mind all the elements of the list should be of the same type or extend the same type. It is not recommended but if you really want to you can create ArrayList that can pretty much hold all different types, Custom objects, Integers, Strings.
2 of 4
1
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.
Find elsewhere
🌐
Lawrence
www2.lawrence.edu › fast › GREGGJ › CMSC150 › 062ArrayLists › ArrayLists.html
Working with ArrayLists
The ArrayList class is a Java class that you can use to store lists of objects.
🌐
Reddit
reddit.com › r/javahelp › creating an array list of objects.
r/javahelp on Reddit: Creating an array list of objects.
December 5, 2024 -

I'm very new to programming, but I have run into a problem that I cant for the life of me figure out why it wont work or how to fix it. I have created a class called resource, and I want to create resource objects then add them to an array list. This is what I have for code.

Resources.java

public class Resources {
public Resource log = new Resource();
public Resource stone = new Resource();
public static ArrayList<Resource> *resourceList* = new ArrayList<Resource>();
resourceList.add(log);
public static void printResources() {
for (Resource val: \*resourceList\* ) {  

System.\*out\*.print(val + " ");  
}
}
}

I can include the Resource class too if that helps, but I'm pretty sure that whatever I'm doing wrong is in this class. I have googled it, but to be honest, I'm still a little foggy on the concepts of objects and classes, so I'm sure it's just something dumb I'm missing.
Thanks in advanced

Top answer
1 of 3
4
What is the error/problem? You absolutely have to include the error message verbatim, as the compiler/runtime gives. Luckily, the problem is easy to spot: you are trying to call a method resourceList.add(log) outside of any method. This does not work in Java. You can only declare variables and initialize them, but not perform other operations outside methods. You will need to add the above method call into some method, e.g. into public static void main (at least for testing). Commonly, you would have a method addResource that takes in a Resource instance (object of type Resource) which does exactly what you do in the above, quoted line - adds the resource to the ArrayList. Alternatively, and I wouldn't recommend it without looking deeper into it, you could use a static initializer block right after your public static ArrayList... declaration. Edit: Sidenote: be wary of making everything static. Don't even get into that habit. Not even at the beginning. Try to avoid it by default. There is nothing wrong with making things non-static and instantiating even a single object of that class. Getting into the habit of not using static will help you extremely much in the long run and will make your OOP programming much easier and better (even though in the beginning it seems to be the opposite).
2 of 3
1
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://i.imgur.com/EJ7tqek.png ) 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.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-array-of-arraylist-of-array
Java Array of ArrayList, ArrayList of Array | DigitalOcean
August 4, 2022 - Below is a simple example showing how to create ArrayList of object arrays in java. import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class JavaArrayListOfObjectArray { public static void main(String[] args) { // list of Object arrays to hold different types of array List<Object[]> list = new ArrayList<Object[]>(); String[] arr1 = { "a", "b", "c" }; String[] arr2 = { "1", "2", "3", "4" }; JavaArrayListOfObjectArray aa = new JavaArrayListOfObjectArray(); JavaArrayListOfObjectArray.A[] arr3 = { aa.new A("AA"), aa.new A("BB") }; list.add(arr1); list.add(arr2); lis
🌐
FavTutor
favtutor.com › blogs › java-array-of-objects
Array of Objects in Java (with Examples)
December 1, 2023 - Arrays of objects in Java are collections that can store multiple instances of a class. They follow a similar structure to arrays of primitive data types but instead hold references to objects.
🌐
University of San Francisco Computer Science
cs.usfca.edu › ~wolber › courses › 110 › lectures › ArrayOfObjs.htm
Java Arrays of Objects
ArrayList and LinkedList are list classes in the Java library. They both take care of allowing the list to grow to any size, and they provide numerous methods to manipulate lists.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-objects-can-an-arraylist-hold-in-java
How Objects Can an ArrayList Hold in Java? - GeeksforGeeks
July 23, 2025 - Second half: Making the same ArrayList with class objects as elements. ... Here <String> represents the data-type of the elements that is to be stored in the ArrayList. Remember that this can not be any primitive data-type such as int or float. Illustration: Sub Class taken for consideration is randomly named 'Person'. Method: List.get() method of List interface in Java is used to get the element present in this list at a given specific index.
🌐
Processing
processing.org › examples › arraylistclass
ArrayList of objects / Examples / Processing.org
*/ ArrayList<Ball> balls; int ballWidth = 48; void setup() { size(640, 360); noStroke(); // Create an empty ArrayList (will store Ball objects) balls = new ArrayList<Ball>(); // Start by adding one element balls.add(new Ball(width/2, 0, ballWidth)); } void draw() { background(255); // With an array, we say balls.length, with an ArrayList, we say balls.size() // The length of an ArrayList is dynamic // Notice how we are looping through the ArrayList backwards // This is because we are deleting elements from the list for (int i = balls.size()-1; i >= 0; i--) { // An ArrayList doesn't know what i
🌐
Unstop
unstop.com › home › blog › array of objects in java | create, sort, and more (+examples)
Array Of Objects In Java | Create, Sort, And More (+Examples)
December 19, 2024 - Similarities Between Java Vs. C++ ... An array of objects in Java is a collection that stores references to multiple objects of the same class.
🌐
Edureka
edureka.co › blog › array-of-objects-in-java
Array Of Objects In Java | Java Object Arrays | Edureka
March 1, 2023 - An object represents a single record in memory, and thus for multiple records, an array of objects must be created. It must be noted, that the arrays can hold only references to the objects, and not the objects themselves. Let us see how can we declare Array of objects in Java.
🌐
Baeldung
baeldung.com › home › java › java list › difference between arrays.aslist() and list.of()
Difference Between Arrays.asList() and List.of() | Baeldung
2 weeks ago - Arrays.asList(), introduced in Java 1.2, simplifies the creation of a List object, which is a part of the Java Collections Framework.
🌐
Stanford
web.stanford.edu › class › archive › cs › cs108 › cs108.1082 › 106a-java-handouts › HO49ArrayList.pdf pdf
CS106A, Stanford Handout #49 Fall, 2004-05 Nick Parlante ArrayList
This objects-only constraint stems from fundamental aspects of the way Java · works, but as a practical matter it is not much of a problem. (Java "arrays" which we will · study shortly are an alternative to the ArrayList, and they can store primitives.) First we will look at a small ArrayList example to see roughly how it works, and then we ... This code creates a new, empty ArrayList... ArrayList list = new ArrayList(); // make a new ArrayList (initially empty)
🌐
GeeksforGeeks
geeksforgeeks.org › java › arraylist-in-java
ArrayList in Java - GeeksforGeeks
This constructor is used to build an array list with the initial capacity being specified. ... Now, Using the constructors we have got ArrayList for further operations like Insertion,Deletion and Updation of the elements in ArrayList. ... import java.util.*; class GFG{ public static void main(String args[]){ // Creating an Array of string type ArrayList<String> al = new ArrayList<>(); // 1.
Published   May 12, 2026