Solved

Java Vector quesion

Posted on 2011-09-07
8
240 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
 
LVL 47

Expert Comment

by:for_yan
ID: 36499384
It is hard to imagine situation where you want to extend Vector
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now