Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

Accessing a vector of vectors of vectors.

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
javaQQ
Asked:
javaQQ
1 Solution
 
objectsCommented:
0
 
InNoCenT_Ch1ldCommented:
(((Vector)theVector.elementAt( 0 )).elementAt(1)).elementAt(2);

OR

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

Vector v2 = v.elementAt(2);


0
 
objectsCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
InNoCenT_Ch1ldCommented:
...guess so ;p
0
 
InNoCenT_Ch1ldCommented:
oopss.. mistake! objects comment is right...
ignore mine
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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
 
tomboshellCommented:
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
 
objectsCommented:
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now