7 Replies - 606 Views - Last Post: 31 March 2010 - 04:54 AM Rate Topic: -----

#1 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

vector of vector

Posted 28 March 2010 - 06:21 AM

Hi,
I want to define a Vector that is of type of a Vector of String.
I did it:
Vector <Vector<String>> musicNames=new Vector <Vector<String>>();


My problem is when I want to enter a Vector to 'albumNames'.
If I don't know how many vectors I want to add to the first Vector(I mean musicNames), or if there are a lot of Vector I want to add to the first Vector, what should I do?
For example for adding 1000 Vectors to 'musicNames' I cannot 'new' another Vector 1000 times and add it to 'musicNames' each time.
I thought and used a loop. but I think it is not correct:
boolean t=true;
while (true)
{
	    Vector<String> musics=new Vector<String>();
            String s1=UserIn.readString();
            String s2=UserIn.readString();
            String s3=UserIn.readString();
            String s4=UserIn.readString();
            //user should enter four string
            musics.addElement(s1);
            musics.addElement(s2);
            musics.addElement(s3);
            musics.addElement(s4);
            //add four String to vector 'albumNames'
            musicNames.addElement(musics);
            System.out.println("If you want to enter musics again enter #");
            System.out.println("If you don't want, enter $");
            char myChar=UserIn.readChar();
            if (myChar=='$')
            	t=false;
            if (t==false)
            	break;
}

Is it correct? Can anyone help me?

I am very new in programming. so please, please, please explain in simple way for me if you can.
thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: vector of vector

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: vector of vector

Posted 28 March 2010 - 08:20 AM

What do you mean by "not correct"?
Are you getting any errors? or not the desired output?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: vector of vector

Posted 28 March 2010 - 11:04 AM

Vector of Vector is usually a sign you're doing it wrong. A two dimensional array, or vector / list equivalent, is ambiguous at best. This is Java, use some class.

Perhaps something like:
class Album {
   public List<String> artists;
   public List<String> songs;
}

List<Album> albums = new ArrayList<Album>();


Was This Post Helpful? 4
  • +
  • -

#4 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: vector of vector

Posted 29 March 2010 - 12:27 AM

View Postbaavgai, on 28 March 2010 - 10:04 AM, said:

Vector of Vector is usually a sign you're doing it wrong. A two dimensional array, or vector / list equivalent, is ambiguous at best. This is Java, use some class.Perhaps something like:

Yes I know. But the problem is my teacher. She wanted my to use vector of vector.

View Postbaavgai, on 28 March 2010 - 10:04 AM, said:

class Album {
   public List<String> artists;
   public List<String> songs;
}

List<Album> albums = new ArrayList<Album>();

Thanks for your answer.
Can you tell me the difference between List and ArrayList?
And about your class? Can you explain it?

This post has been edited by saSBH2: 29 March 2010 - 12:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: vector of vector

Posted 29 March 2010 - 12:35 AM

Quote

What do you mean by "not correct"?
Are you getting any errors? or not the desired output?


not the desired output. but i'm not sure. maybe it's because of another part of my program. i must check it carefully.

what i want to know is: isn't it a problem that I 'new' my vector with the same name several times(in the loop). For example is there any difference between these:
1:
Vector<Vector<String>> myVector=new Vector<Vector<String>>();
    Vector<String> name1=new Vector<String>();
    String s1=s.next();
    String s2=s.next();
    name1.addElement(s1);
    name1.addElement(s2);
    Vector<String>name2=new Vector<String>();
    String s3=s.next();
    String s4=s.next();
    name2.addElement(s3);
    name2.addElement(s4);
    myVector.addElement(name1);
    myVector.addElement(name2);

2:
 Vector<Vector<String>> myVector=new Vector<Vector<String>>();
    for(int j=0;j<2;j++)
    {
    	Vector<String> name=new Vector<String>();
    	String son=s.next();
    	String sin=s.next();
    	name.addElement(son);
    	name.addElement(sin);
    	myVector.addElement(name);
    }

Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: vector of vector

Posted 29 March 2010 - 04:02 AM

View PostsaSBH2, on 29 March 2010 - 01:27 AM, said:

View Postbaavgai, on 28 March 2010 - 10:04 AM, said:

Vector of Vector is usually a sign you're doing it wrong. A two dimensional array, or vector / list equivalent, is ambiguous at best. This is Java, use some class.Perhaps something like:

Yes I know. But the problem is my teacher. She wanted my to use vector of vector.


I'm sorry, it happens. What does is mean, though? The strings are songs? What is the meaning of each vector of songs; you must be grouping them based on something.

View PostsaSBH2, on 29 March 2010 - 01:27 AM, said:

Can you tell me the difference between List and ArrayList?
And about your class? Can you explain it?


A List is actually an interface. An ArrayList is one of the many implementations of that interface. More here: http://java.sun.com/.../util/List.html

Using the least you need in programming means that debugging is easier down the line. It's why you encapsulate in objects and scope narrowly. Here, I don't need all the methods of ArrayList, just the ones defined in List.

The class is really just a partial, it needs a constructor. A couple methods wouldn't hurt, either. We're assuming here that you have a object Album that can be associated with multiple artists and contain multiple songs. And that those things are just strings. A more complete implementation might look something like this:

class Album {
	private String name;
	private List<String> artists;
	private List<String> songs;
	
	public Album(String name) {
		this.name = name;
		artists = new ArrayList<String>()
		songs = new ArrayList<String>()
	}
	public String getName() { return name; }
	public List<String> getArtists() { return artists; }
	public List<String> getSongs() { return songs; }
	
	public void addArtist(String value) { artists.add(value); }
	public void addSong(String value) { songs.add(value); }
}

//....

Vector<Album> albums = new Vector<Album>();

Album album = new Album("Barcelona");
album.addArtist("Freddie Mercury");
album.addArtist("Montserrat Caballé");
album.addSong("Barcelona");
album.addSong("La Japonaise");
album.addSong("The Fallen Priest");
album.addSong("Ensueño");

albums.add(album);



While a song can have more than one artist, well, what can I tell you; first draft.
Was This Post Helpful? 1
  • +
  • -

#7 saSBH2  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: vector of vector

Posted 31 March 2010 - 12:53 AM

Thanks for your information, baavgai . I will read more about it.


Quote

I'm sorry, it happens. What does is mean, though? The strings are songs? What is the meaning of each vector of songs; you must be grouping them based on something.


I asked my question in post #5.
Are those two codes the same?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: vector of vector

Posted 31 March 2010 - 04:54 AM

View PostsaSBH2, on 31 March 2010 - 01:53 AM, said:

I asked my question in post #5.
Are those two codes the same?


Since I don't really care about input, let's take a look with minor rearrangement.
1:
Vector<Vector<String>> myVector=new Vector<Vector<String>>();

Vector<String> name1=new Vector<String>();
name1.addElement(s1);
name1.addElement(s2);
myVector.addElement(name1);

Vector<String>name2=new Vector<String>();
name2.addElement(s1);
name2.addElement(s2);
myVector.addElement(name2);



2:
Vector<Vector<String>> myVector=new Vector<Vector<String>>();
for(int j=0;j<2;j++) {
	Vector<String> name=new Vector<String>();
	name.addElement(s1);
	name.addElement(s2);
	myVector.addElement(name);
}



Yep, same.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1