0

I am storing data in class object from array List like

MyClass[] obj = list.toArray(new myclass[0]);   

if i want to append ArrayList data into MyClass object. what should i need to do? i want to do somethink like

obj = obj.append(list);
7
  • what do you want to say exactly? Commented May 9, 2014 at 11:59
  • how can i append to class object from arraylist? Commented May 9, 2014 at 12:01
  • can you elaborate your question and your basic requirement,i will give you any alternative solution. Commented May 9, 2014 at 12:03
  • you can try getter,setter method to add data to your class Commented May 9, 2014 at 12:06
  • i have each time array list and just want to append in exit object. Commented May 9, 2014 at 12:07

2 Answers 2

1

The length of an array is immutable in java. This means you can't change the size of an array once you have created it. So if you don't know the final size of the array you can't do what you want using Array.

When you do:

MyClass[] obj = list.toArray(new myclass[0]);

You are creating an Array with size=1. Then you can't add elements.

Using List instead of Array could be a good solution.

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

2 Comments

now i have just two custom class object and want to add both. i mean copy one to another
Sorry but I don't see what you mean. You have to different kinds of Objects and you want add one of them to the other. Or same Objects?
0

I think you are mistaken ArrayList with array. ArrayList is a raw type meaning it needs parameter to instantiate like:

ArrayList<String> arrayList = new ArrayList<String>();

which creates an empty ArrayList which will contain String. You can add String by using arrayList.add("Something");. Now your array contains 1 element (String).

On the other hand assuming list is an ArrayList list.toArray() method will return an array (and does not take any argument). I don't think you want something like this.

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.