• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Copy existing Array into new ArrayList

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi -

I'm trying to copy an existing Array into an ArrayList that is in a different class. I think I'm close, but I keep getting compiler issues. Can you tell me what I may be missing?



[ October 01, 2008: Message edited by: Carie Adasek ]

[edit]Add newlines to break long lines. CR[/edit]
[ October 01, 2008: Message edited by: Campbell Ritchie ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
posting the exact text of the error message helps...
 
Carie Herrick
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the error from the compiler:

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Where is the type T defined? It is not a type specified for the class. It is not a type specified for the method. How does Java know what type is it?

Henry
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need to use the Object[] part if you declare it - either as a variable or a parameter. So only in "public NewObjectData(Object[] objects)" should you use it.

Now if you fix this (and the other errors related to the adding), your code will still not compile. The reason is that your ArrayList is generic with type T, and Object may not match T - the compiler can't tell at compile time.

Only if you change Object[] to T[] will you be able to solve this. Well, that, or stop using ArrayList<T> and start using something more generic like ArrayList<Object> - that can store anything.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no ArrayList constructor that accepts an array argument. You can convert the array to a List using java.util.Arrays.asList() or you can *manually* insert each element of an array into a newly created ArrayList. You cannot pass an array to an ArrayList constructor.
 
Carie Herrick
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I took the advice to not use T and use <Object>. I did the below, and this compiled, but no data was loaded. Am I missing something else?

 
Carie Herrick
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:
There is no ArrayList constructor that accepts an array argument. You can convert the array to a List using java.util.Arrays.asList() or you can *manually* insert each element of an array into a newly created ArrayList. You cannot pass an array to an ArrayList constructor.



This doesn't make sense to me, because my requirements state: "Use an ArrayList object. Its constructor has an 'Object' array (ie Object [] objects) as its parameter. It creates the ArrayList object, then in a loop, it adds the 'Object' objects from the array to the ArrayList." (I've replaced the original class name with 'Object' - I hope this isn't too confusing.)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can add arrays to an ArrayList, easy.And an array counts as an Object, so it will add nicely.
But go and have a look at the API for constructors, and you see what Garrett was telling you. Check very carefully what you are supposed to do, before going round saying there are mistakes in the assignment specification. Your lecturer will probably have run the exercise and know what works.

What's more, we are simple naive souls who believe everything you tell us; if you write Object[] we think you mean Object[]; if you meant a different class, please write Foo[] because we all know Foo isn't a real class (well, not usually).

If you read carefully what you have posted, you are traversing the array with a for-each loop where you call the elements "item" and adding "objects."

I still think you would have been better parameterising your class so you can use T[] or T as Rob first suggested.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what to think of your comment at this line:

Is the ArrayList class you're using a java.util.ArrayList? or has your instructor provided you with a custom class that is also called ArrayList?
 
Carie Herrick
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I have used import java.util.ArrayList; for this.

I've tweeked my code some, but it's still not loading the data. I figured out that my <object> is the class that I'm using to load, so I replaced it with <Foo>.

Where I have the //no parameter as will accept default... Is this why it's not loading the data? Do I need a parameter here that calls something?

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Garrett Rowe:
I'm not sure what to think of your comment at this line:



It means she has used the no-args constructor because the default size of 10 is suitable.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not surprised you aren't adding anything; look at your for-each loop and see where you are getting the elements from
 
Carie Herrick
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I finally figured it out. I had to return the array and define it before I could add it to the ArrayList.

Got it working. Thanks for the comments - got me thinking!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carie Adasek:
Got it working.

Well done

Originally posted by Carie Adasek:
Thanks for the comments - got me thinking!

That is the idea; you will remember it a lot better next time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic