Solved

Java Vector quesion

Posted on 2011-09-07
8
245 Views
Last Modified: 2012-05-12
I am creating a class that extends Vector. The vector will consists of 4 elements in this order:
String, String, Date, double

How do I set this up?
0
Comment
Question by:allelopath
8 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 36499373
>>I am creating a class that extends Vector.

Why? ;)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36499375
I don't think you need to extend Vector

You rather make a class


public class  FourElements {
String one;
AString two;
Date d;
double x;

}


then you want constructor etc.

and then you make Vectorb which would hole ielements - each one ionstance of your class




}
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36499381
Or you can have class which will have Vectopr with your four element as
varaiable so it will be not inheritance but

HAS a Vector

relationship
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 47

Expert Comment

by:for_yan
ID: 36499384
It is hard to imagine situation where you want to extend Vector
0
 
LVL 47

Accepted Solution

by:
for_yan earned 300 total points
ID: 36499828
seee here a rather good discussion which comes to the conclusin that it is normally not a good
idea to extend Vector:

http://forums.whirlpool.net.au/archive/1309754



The author of this code [whic was extending Vector, see link above] was probably thinking something along the lines of "a product catalog is a kind of vector, since it stores a bunch of products, just like a vector stores a bunch of items".

The "is a kind of" relation is most often represented with inheritance.

Unfortunately, the author of this code made the wrong decision. A product catalog is not a kind of vector:

    A product catalog doesn't necessarily entail a particular ordering between its entries; a vector does.
    A product catalog has certain rules and constraints about its entries, such as that every element must be a Product, and that the catalog should only be modified in ways that maintain these constraints. A vector doesn't have these rules and constraints.

For these reasons, this ProductCatalog class does not meet the second requirement in my previous post.

At another level, it's a wrong design decision because they're locking in the implementation of their product catalog in such a way that it'd be particularly hard to change in the future.

Even if it is thought that it will not need to be changed in the future, it's the wrong data structure anyhow: a product catalog would probably need to be searched with a variety of different criteria (e.g. by product type, by price, by colour), and a single vector does not make this straight-forward.
0
 
LVL 10

Assisted Solution

by:gordon_vt02
gordon_vt02 earned 100 total points
ID: 36504603
Also, its generally much better to use one of the other List implementations than Vector -- using the utilities in java.util.concurrent if you need synchronized access to the List.  Vector does a lot of internal synchronization which makes it much slower when only accessed by a single thread.
0
 
LVL 1

Author Comment

by:allelopath
ID: 36511220
ok, it was a stupid idea, driven by a DefaultTableModel constructor :
DefaultTableModel(Vector data, Vector columnNames)
but still not a good way to go.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36511564
>>ok, it was a stupid idea, driven by a DefaultTableModel constructor :

If you'd answered my question and told me that right away, i would have answered differently - and differently from any of the other answers above ;)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

777 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