Solved

Accessing a vector of vectors of vectors.

Posted on 2003-12-11
8
359 Views
Last Modified: 2010-03-31
If this post is a duplicate, MY APOLOGIES. I have not used this forum in months, and cannot find any trace of my earlier attempt to post this query.

I have two related questions regarding Vectors.

Question 1:
This code gives me the second value in the first element of a vector of vectors
((Vector)theVector.elementAt( 0 )).elementAt(1);

My question: How do I extend this for one more level (to a vector of vectors of vectors)?
For example, how do I get the the third value in the second vector of the first vector?

Question 2:      
Code like the following      
variableX = (String)((Vector)theVector.elementAt( 0 )).elementAt(3);      

causes a "java.lang.ClassCastException" error:

apple.awt.EventQueueExceptionHandler Caught Throwable: java.lang.ClassCastException

Am I right in assuming that this is the reuslt of the code pointing to the wrong level of vector and
can be solved by the code which ppoints to the right level of vector?      


Many thanks in advance.




0
Comment
Question by:javaQQ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9925749
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9925755
(((Vector)theVector.elementAt( 0 )).elementAt(1)).elementAt(2);

OR

Vector v = ((Vector)theVector.elementAt( 0 )).elementAt(1);

Vector v2 = v.elementAt(2);


0
 
LVL 92

Accepted Solution

by:
objects earned 60 total points
ID: 9925765
Q1.

((Vector)(((Vector)theVector.elementAt( 0 )).elementAt(1))).elementAt(0);

Q2. No it means you are trying to incorrectlt cast.
ie. The instance you are casting is not what you are casting it to.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9925766
...guess so ;p
0
 
LVL 3

Expert Comment

by:InNoCenT_Ch1ld
ID: 9925772
oopss.. mistake! objects comment is right...
ignore mine
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9926015
How many places have you asked the same question?

http://www.experts-exchange.com/Programming/Q_20823608.html

You could have given a link to this question with only some 10 or 20 points over there instead of asking the same thing and also giving 60 points. You have some responses there too.

Mayank.
0
 
LVL 7

Expert Comment

by:tomboshell
ID: 9926613
ClassCastException that means you did not get back what you thought you would.  Try using the instanceof operator to stop that. Try something like the following
// old call
//variableX = (String)((Vector)theVector.elementAt( 0 )).elementAt(3);

public String findValue(Vector vIn, int row, int column){
    String sReturn = null;
    try{
       Object o = vIn.elementAt(row);
      if(o instanceof Vector){
        o = ((Vector)o).elementAt(column);
        if(o instanceof String) sReturn = (String)o;
        else sReturn = "";
       } else if(o instanceof String){
        sReturn = (String)o;
      } else sReturn = ""; // probably better to create and throw your own exception here.  Something like "ObjectUnknownException"  
    }catch(ArrayIndexOutOfBoundException aiob){
      System.err.println("huh! Not enough data to look through!");
      aiob.printStackTrace();
   }
  return sReturn;
}


Or something like that.
0
 
LVL 92

Expert Comment

by:objects
ID: 10276548
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 77
How  can  i  resolve  HTTP Status 404 -? 8 59
java imports not found 4 49
Website checklist for browser compatibility? 2 38
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question