parsa
parsa

Reputation: 49

define an array which its elements are object in java

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?

Upvotes: 0

Views: 97

Answers (3)

afrin216
afrin216

Reputation: 2335

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

Upvotes: 0

zellio
zellio

Reputation: 32484

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.

Upvotes: 2

Ahmad
Ahmad

Reputation: 2190

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

Upvotes: 0

Related Questions