0

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);

Can anyone help me on this?

3 Answers 3

2

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

List<Object> object = new ArrayList<Object>();

Which will give you the .add functionality because it's a List.

Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

0

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();
}

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.