This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[newbie] Vector of arrays...how to add them

 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea how to add some object to my Vector? As you might notice I'm trying to address what seems to be a common issue...a dynamically expanding array of objects, ideally of any type.



Related links: http://www.javaworld.com/javaworld/jw-08-1999/jw-08-cooltools.html
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to mix a List of arrays of the primitive ints (int[]) with arrays of the Object Integer (Integer[]). An Integer is not the same as an int. You should make them both refer to the same data type:



- or -

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jon Camilleri wrote:Any idea how to add some object to my Vector?

Um, have you tried the add() method?

Jon Camilleri wrote:As you might notice I'm trying to address what seems to be a common issue...a dynamically expanding array of objects, ideally of any type.

A common issue twenty or more years ago, maybe.

I tried reading the ten-year-old article you linked to, but I nearly died of boredom halfway through. I'm still recovering.

Look, whatever ancient text you're reading that talks about Vectors, please, just burn it and upgrade to something published in the current millenium.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:. . . in the current millenium.
If we are sticking to things from the current millennium, don't use Vector. It is generally regarded as legacy code. Use one of the more modern implementation of List<E>. Most likely ArrayList<E>.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that was my point. One of them, at least.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your updates Well, actually I did try a modern day ArrayList, which is one of my favorite constructs in other languages, but I can't find a way of creating an array of integers:



Related Links:
1. http://www.roseindia.net/java/beginners/array_list_demo.shtml
2. The API http://java.sun.com/javase/6/docs/api/


Well the "solution" was quite simple... An ArrayList made up of an ArrayList of Integer (not int)



 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That roseindia demonstration was not really useful. It didn't tell you to declare your object by its interface. You would do better to declareFor some reason, which I don't know, I am getting >< when I typed <. Readers: please check for that error.

Anyway, why don't you declare the List to take int[] elements?
java IntArrayListDemo 123 234 345 456 567 678 789
And see for yourself what happens.
 
Jon Camilleri
Ranch Hand
Posts: 664
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:That roseindia demonstration was not really useful. It didn't tell you to declare your object by its interface. You would do better to declareFor some reason, which I don't know, I am getting >< when I typed <. Readers: please check for that error.

Anyway, why don't you declare the List to take int[] elements?
java IntArrayListDemo 123 234 345 456 567 678 789
And see for yourself what happens.


Thanks, that seemed to work
My code did not seem to work when I tried implementing something on the same lines of thought (see above posts).



 
Campbell Ritchie
Sheriff
Pie
Posts: 50239
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic