Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I want to define an array which its elements are object in java and i can use for ex this method: public void add(Object elem);
public void add(Object elem);
Can anyone help me on this?
A literal object array is defined as any other array.
Object[] objects = new Object[10];
Though, more likely, you are looking for something like ArrayList
ArrayList
List<Object> object = new ArrayList<Object>();
Which will give you the .add functionality because it's a List.
.add
List
Add a comment
Objecttype objects= new Objecttype [initial size]
Why you want to use arrays. go for ArrayList in which you don't bother to define your initial size also it provides its own add method
Defining an array of objects is just like defining any other array
Object[] objectList = new Object[MAX_LIMIT]; for(int i=0;i<objectList.length;i++){ objectList[i] = new ANY_DATA_TYPE(); }
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.