Using Vectors

Put your problem here if it does not fit any of the other categories.

Using Vectors

Postby bluefloyd8 » Tue Mar 04, 2008 8:29 am

Im coming over from C to Java so I dont know a lot about vectors. But im trying to make a dynamic array using vectors and it isnt working.

Is the following incorrect to use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Vector ParsedSiteInfoVector = new Vector();
  2.  
  3.  
  4.  
  5. ParsedSiteInfo x = new ParsedSiteInfo();
  6.  
  7. this.ParsedSiteInfoVector.addElement((ParsedSiteInfo) x);
  8.  
  9.  
  10.  
  11. public ParsedSiteInfo getParsedData(int i) {
  12.  
  13.          // return this.parsedInfo[i];
  14.  
  15.          ParsedSiteInfo t;
  16.  
  17.          t = (ParsedSiteInfo) ParsedSiteInfoVector.elementAt(sites-1);
  18.  
  19.          return t;
  20.  
  21. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Thank you for any help
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Top

Postby plusminus » Tue Mar 04, 2008 1:30 pm

Hello bluefloyd,

I usually work with ArrayLists. You should also use Generics (in C++ called Templates..?) for compiletime Type-safety.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ArrayList<ParsedSiteInfo> parsedSiteInfos = new ArrayList<ParsedSiteInfo>();
  2.  
  3.  
  4.  
  5. ParsedSiteInfo x = new ParsedSiteInfo();
  6.  
  7. this.parsedSiteInfos.add(x);
  8.  
  9.  
  10.  
  11. public ParsedSiteInfo getParsedData(int i) { // i isn't used ?
  12.  
  13.          // return this.parsedInfo[i]; <-- this was your C code ?
  14.  
  15.       return parsedSiteInfos.get(sites-1);
  16.  
  17. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Btw: you didn't say what is not working.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby bluefloyd8 » Tue Mar 04, 2008 9:36 pm

I wrote that code from memory. Ignore sites and use 'i' instead.

The problem is the vector is empty; even after I call AddItem() several times.
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Postby plusminus » Wed Mar 05, 2008 12:01 pm

Hello bluefloyd,

I'd smash my nuts, if this does not work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         ArrayList<Object> objList = new ArrayList<Object>();
  2.  
  3.         objList.add(new Object());                                             
  4.  
  5.         Object o = objList.get(0);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby bluefloyd8 » Wed Mar 05, 2008 4:46 pm

I got it working with ArrayList instead of Vector.
thanks +-
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Postby plusminus » Wed Mar 05, 2008 5:00 pm

You saved me ;)
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby bluefloyd8 » Wed Mar 05, 2008 11:17 pm

Errr. wait. no it didnt work. Smash your nuts!

(kidding)
User avatar
bluefloyd8
Experienced Developer
Experienced Developer
 
Posts: 70
Joined: Tue Jan 22, 2008 3:57 am
Location: Indiana, USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 29 guests