Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-17-2005, 04:51 AM   #1 (permalink)
inox
Registered User
 
Join Date: Feb 2005
Posts: 5
inox is on a distinguished road
Vector of vectors

Hello everybody,

I am trying to figure out how I can have vector of vectors. Basically what I want to do is to have something like below

VectorN = [ [vector_a],[vector_b],[vector_c],...,[vector_n] ]

VectorN is comprises of a series of another vector where each consists of 3 values ([vector_a] = [int x, int y, int z]

I really appreciate the helps.

Thanking in advance :-)
inox is offline   Reply With Quote
Old 02-17-2005, 05:12 AM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
A Vector is an Object. Vectors contain Objects. Ergo, a Vector can contain Vectors just like any other Object.

It sounds like you might want to create a new class to contain your values rather than use a Vector. Unless you're using Java 1.5, you'll have a lot of casting back and forth, as those ints will need to be put into Integers and then pulled out.

You can then put your new class into a Vector (although I'd suggest ArrayList as a lighter-weight class).
__________________
GitS
Belisarius is offline   Reply With Quote
Old 02-17-2005, 05:23 AM   #3 (permalink)
inox
Registered User
 
Join Date: Feb 2005
Posts: 5
inox is on a distinguished road
Hi Belisarius,

Thanks for your prompt reply. I am currently using Java 1.4. If I still want to use Vector, do you suggest I'll have Java 1.5 instead? Or is it more advisable for me to work with ArrayList? Can ArrayList provide me with the same result as I want like the one in Vector?

Actually I want to store each rgb values for a pixel of an image into say a Set so that any rgb with redundant values will be omitted which allow me to count the numbe of colours in the image. I later would like to have access again to these values for further processing.

Your idea(s) are most welcome and indeed needed.

Thanks...
inox is offline   Reply With Quote
Old 02-17-2005, 08:02 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,175
Belisarius is on a distinguished road
Vector is just an old class that was jury-rigged into the collections framework. ArrayList is basically the same thing, only built from the ground up to be part of Collections. Vector has been kept around for backwards compatability, but it's a little bit heavy for what most people want to use it for.

Now, what I would do is write yor own Pixel object.

Code:
public class Pixel extends java.awt.Point implements Comparable{
  private int red;
  private int green;
  private int blue;
  
  public Pixel() {
    super();
    init(0,0,0,0,0);
  }
  
  public Pixel(int red, int green, int blue){
    super();
    init(0, 0, red, green, blue);
  }
  
  public Pixel(int x, int y, int red, int green, int blue){
    super();
    init(x, y, red, green, blue);
  }
  
  private void init(int x, int y, int red, int green, int blue){
    super.setLocation(x, y);
    this.red = red;
    this.green = green;
    this.blue = blue;    
  }
  
  public boolean compareTo(Pixel input){
    // Write your compareTo method here
  }
  
  public String toString(){
    // Write your toString method here    
  }
  
  public boolean equals(Pixel input){
    // Write your equals method here
  }
  
  public int hashCode(){
    // Write your hashCode method if you plan on using something that requires
    // a hash.
  }

  public int getRed() {
    return red;
  }

  public void setRed(int red) {
    this.red=red;
  }

  public int getGreen() {
    return green;
  }

  public void setGreen(int green) {
    this.green=green;
  }

  public int getBlue() {
    return blue;
  }

  public void setBlue(int blue) {
    this.blue=blue;
  }
}
Be sure to write the compareTo() and equals() methods. You can then add them to a Set (say . . . TreeSet). This will store only unique Pixels. If you want, you can write your own Comparator and pass it to the TreeSet constructor should you wish to consider *only* colors and not position, while not tainting the default method of comparison for Pixel. In other words, pixels with the same RGB values but different XY values would be, by default, different. If you only wanted to consider similar RGB values, you could write an anonymous Comparator, and pass it to the TreeSet when you create it. It would then include only unique RGB values, regardless of XY position.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 02-18-2005, 03:46 AM   #5 (permalink)
inox
Registered User
 
Join Date: Feb 2005
Posts: 5
inox is on a distinguished road
Thanks Bel for the lengthy explanation and of course the pixel object class. Really appreciate it. I will try this straight away.

Last edited by Belisarius; 02-18-2005 at 10:55 AM. Reason: Name's Belisarius, GitS is a TV show.
inox is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Multi Dimensional Array using Vectors. abs Standard C, C++ 16 02-26-2005 05:40 AM
vector of vectors zergmuncher Standard C, C++ 5 05-15-2003 09:15 AM


All times are GMT -8. The time now is 12:52 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting