Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 13th, 2002, 02:28 AM
grahowler grahowler is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 91 grahowler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Vector of vectors

My question is not a server side question as much as a general Java question about using a vector of vectors built up from parameters passed from an applet as follows...

<applet
code=Applet1.class
name=Applet1
width=400
height=300 viewastext>

<param name=item1 value="item1">
<param name=item1sub1 value="item1 - sub1">
<param name=item1sub2 value="item1 - sub2">
<param name=item1sub3 value="item1 - sub3">

<param name=item2 value="item2">
<param name=item2sub1 value="item2 - sub1">
<param name=item2sub2 value="item2 - sub2">

<param name=item3 value="item3">
<param name=item3sub1 value="item3 - sub1">

</applet>

************************************************
I can successfully build a vector of vectors however I am having problems accessing the nested vectors in the following java code...
************************************************

import java.awt.*;
import java.applet.*;
import java.util.*;

public class Applet1 extends Applet
{
Vector v1 = new Vector();
int iItemCount = 1;

public void init()
{
String sTemp;
while(getParameter("item"+ iItemCount) != null)
{
Vector v2 = new Vector();

int iSubItemCount = 1;
while(getParameter("item"+ iItemCount +"sub"+ iSubItemCount) != null)
{
sTemp = getParameter("item"+ iItemCount +"sub"+ iSubItemCount);
v2.addElement(sTemp);
iSubItemCount++;
}
iSubItemCount = 1;

v1.addElement(v2);
iItemCount++;
}
iItemCount = 0;
}

public void paint(Graphics g)
{
for(int iCounter1 = 0; iCounter1 < v1.size(); iCounter1++)
{
/*** This displays the object as if it where some form of array ***/
g.drawString("Vect"+ iCounter1 +" - "+ v1.elementAt(iCounter1).toString(),5,(iCounter1 * 15) + 20);
/*** Does this show that the object is a vector? ***/
g.drawString("Vect"+ iCounter1 +" - "+ v1.elementAt(iCounter1).getClass(),5,(iCounter1 * 15) + 80);
}
}
}

************************************************

When accessing the first vector (v1) it appears as though the object that is at any index of the v1 is in fact a vector from the getClass() method, however when I attempt to use it as such I get a "cannot convert Object to Vector" error message. I have tried everything including playing around with arrays but to no avail.

Am I taking the totally wrong approach or have I missed something obvious?

Thx

Reply With Quote
  #2  
Old December 14th, 2002, 04:06 PM
Shocka's Avatar
Shocka Shocka is offline
dont click here
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 409 Shocka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 35 m
Reputation Power: 7
well i took a look at your code and i think i may know whats wrong..

I am assuming that you are feeding objects of Type String into the Vector v2, and objects of Type Vector into V1

to print it out you need to remember when getting elements from a vector you need to typecast, and i feel this is where your error is.

for arguments sake try somethign like this and see if it work
I am using System.out change this for your implementation.

Code:
for(int i=0; i<v1.size(); i++){
  Vector temp = (Vector)v1.elementAt(i);
  for(int c=0; c<temp.size(); c++){
    String curValue = (String)temp.elementAt(c);
    System.out.print(curValue + '\t');
  }
  System.out.println();
}


it should print out. You will notice that I typecast the vector from v1 as temp then typecast a string from temp.

Reply With Quote
  #3  
Old December 21st, 2002, 02:19 PM
grahowler grahowler is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 91 grahowler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Thumbs up AHA!

Yes that has done the trick! I knew it would be something simple... thanks a million.

Cheers

grahowler

Reply With Quote
  #4  
Old December 22nd, 2002, 09:44 PM
Shocka's Avatar
Shocka Shocka is offline
dont click here
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 409 Shocka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 35 m
Reputation Power: 7
Thumbs up


Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Vector of vectors


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway