• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Create and Add Data or Arrays to 2D vectors

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am familiar with how to add array data to 2D vector in C++. (I do realize Java's Vector is essentially replaced with ArrayList, etc....
I just want to clarify the "mechanics" of how to fill a vector of vectors)

An example of a C++ program is:

CODE BEGIN

#include <iostream>
#include <vector>

using namespace std;

int main()
{
// declare arrays to load into 2D vector
int typeArray[4] = {55,66,77,88};
int valArray[13] = {1,2,3,4,5,6,7,8,9,10,10,10,11};

// declare int type vector of vectors
vector< vector <int> > myVector(4, vector<int> (14,0));

// assign one array per ROW
for (int i = 0; i < myVector.size(); i++) {

myVector[i][0] = typeArray[i];

for (int j = 1; j < myVector[i].size(); j++) {

myVector[i][j] = valArray[j - 1];

}

}

// print vector to screen with 4 ROWS, 13 COLUMNS

for (int k = 0; k < myVector.size(); k++) {

for (int l = 0; l < myVector[k].size(); l++) {

cout << myVector[k][l] << ' ';
}
cout << '\n';
}

cout << " yes" << endl; // generic addon to indicate end of program on console screen
system("Pause"); // to view output on console screen
return 0;

} // end main
~~~~~~~~~~~~~~~~~~~~~~~~~~
OUTPUT

55 1 2 3 4 5 6 7 8 9 10 10 10 11
66 1 2 3 4 5 6 7 8 9 10 10 10 11
77 1 2 3 4 5 6 7 8 9 10 10 10 11
88 1 2 3 4 5 6 7 8 9 10 10 10 11
yes
Press any key to continue . . .
~~~~~~~~~~~~~~~~~~~~~~~~~~~

A similar starting example in Java would be:



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****My first question is how do I add row and column definitions within the vector declaration as in Cplusplus?*****

Cplusplus example defined:
// 4 is the length or number of rows; 14 is the width of number of columns; '0' is the value all cells are initialized to
vector< vector <int> > myVector(4, vector<int> (14,0));


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I've tried working with the code in the link below and am not able to output rows and columns.
http://www.java2s.com/Tutorial/Java/0140__Collections/MultidimensionalVectorsVectorsofVectors.htm


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The output is only:

A

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks in advance for the feedback.


 
Roberta Fine
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for the last code block, I realize that if I add another string to include element 1,
as in:
String t = (String) ((Vector) outer.get(0)).get(1);
it outputs both A & B.
However, I don't understand how this example is of a multidimensional vector.
 
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please avoid too many // comments; they make the lines too long; I have sorted that out for you.

Java arrayed Lists don't have fixed row and column sizes. They increase their capacity as they are filled. If you need to increase the capacity to make them large, use this method. You will discover many of us think that method is an example of poor design if you look here. You can also pass an initial capacity to the appropriate constructor.

Don't say 2D vectors; they are not 2D but vectors of vectors. What is wrong with this sort of thing?Generics would allow you to avoid all those nasty casts.
 
Roberta Fine
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info about comments, I'll keep them brief... ; )
And, I know it's vector of vectors rather than 2D, I caught that after I posted, thanks for the reminder.

As for the code you posted, I put it in Eclipse and had to make a couple of tweaks:



~~~~~~~~~~~~~~~~~~~~~~~~

Output is:

[Campbell, Ritchie, Roberta, Fine]

~~~~~~~~~~~~~~~~~~~~~~~~

I'm not sure if I tweaked your code correctly, that's what the compiler wanted.
If I did it right, then the output isn't what I'm aiming for. I'd like no brackets,
and my name under your name. It might be the "addAll" method, however, Eclipse would not take "add."

Also, I am moving toward making a deck of cards. Per the API, http://www-scf.usc.edu/~jkershaw/hw3/Deck.html#createDeck%28%29

createDeck

public java.util.Vector createDeck()

This method is used to create the actual deck of cards to be used in the game. It creates 4 sets of 13 cards to represent every card with suits. then two jokers are added at the end.

Returns:
This method will return a Vector with all the card data stored within it.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Therefore, I would like to make the deck myself using two arrays and then load them into a vector.
Why, because a vector resizes dynamically which is useful when dealing out cards.
Also, the same card won't get dealt twice.
So, would an ArrayList accomplish these same tasks?
Thanks in advance!


 
Campbell Ritchie
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roberta Fine wrote: . . . that's what the compiler wanted. . . .

In which case the compiler is mistaken.

What you commented out is a second declaration of line, which you mustn't do. It should read line = new ArrayList<>(); without the type at the beginning.
What you are doing is completely different from what I posted. You are filling a List, then adding all its elements to another List. As you see, that works, but produces something totally different, viz a List of Strings. What I showed was a List of Lists of Strings, which is what you asked about initially.

And the code I showed you would work if put inside an appropriate context.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roberta Fine wrote:Also, the same card won't get dealt twice.
So, would an ArrayList accomplish these same tasks?


Not by default. You'll need a Set if you want your collection to disallow duplicates.

Therefore, I would like to make the deck myself using two arrays and then load them into a vector.
Why, because a vector resizes dynamically which is useful when dealing out cards.


I hope you mean a 'vector' in the generic sense, and not java.util.Vector, which is basically a legacy class. ArrayList is almost always preferable to a Vector.

I'm also not quite sure why you would need two arrays. A standard playing deck consists of 52/54 cards, each of which is unique (apart from the Jokers, but you could make those "unique" too if you want).

Now each card does have at least 2 attributes: Suit and Value/Rank, but I think you might find something like an enum better for storing them than an array. Indeed, there are quite a few pages on Google that show you how to build decks of cards with them; this being just one of them (I believe the JLS also has an example).

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . (I believe the JLS also has an example).

HIH

Winston

You believe correctly; here it is.
 
Roberta Fine
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What I showed was a List of Lists of Strings, which is what you asked about initially.



So, I looked at this link:
http://stackoverflow.com/questions/1474954/working-with-a-list-of-lists-in-java
and it showed a different way of declaring a list of lists.
If I tweak the line of code that is in the post from:

to:

and then include the rest of the post's code (although I had to add the datatype of String:

then it works and the output is:
~~~~~~~~~~~~~~~~~~~~~
[[Campbell, Ritchie], [Roberta, Fine]]
~~~~~~~~~~~~~~~~~~~~~
Is this what you mean? I definitely want to learn it correctly, thanks...
 
Roberta Fine
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I hope you mean a 'vector' in the generic sense, and not java.util.Vector, which is basically a legacy class. ArrayList is almost always preferable to a Vector.


Yes, I do mean 'vector' in the generic sense, I do realize that java.util.Vector is basically a legacy class.
However, I'm always up for reminders, pointers, so thank you for the tip.
As for ArrayLists, I am very new to them. I just posted an answer to Sheriff Ritchie with edited code,
so I can answer that more easily after I get his response.


I'm also not quite sure why you would need two arrays. A standard playing deck consists of 52/54 cards, each of which is unique (apart from the Jokers, but you could make those "unique" too if you want).


I'm thinking along the lines of this link, http://www.oopweb.com/Java/Documents/ThinkCSJav/Volume/chap11.htm.
It says:
"If we want to define a new object to represent a playing card, it is pretty obvious what the instance variables should be: rank and suit. It is not as obvious what type the instance variables should be. One possibility is Strings, containing things like "Spade" for suits and "Queen" for ranks. One problem with this implementation is that it would not be easy to compare cards to see which had higher rank or suit."
Does this make sense?
~~~~~~~~~~~~~~~~~~~~~~
And the link you posted is great, http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#d5e12291.
It's a bit beyond my expertise right now, however, the only way to learn it is to practice it.
~~~~~~~~~~~~~~~~~~~~~~
Please advise on using integers for the rank and suit values. I'm not exactly sure how an enum would work in this case.
Thanks in advance.


 
Campbell Ritchie
Marshal
Posts: 80244
426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roberta Fine wrote: . . . this link:
http://stackoverflow.com/questions/1474954/working-with-a-list-of-lists-in-java
. . .
Is this what you mean? I definitely want to learn it correctly, thanks...

That link looks very similar to what I posted. There are two major differences:
  • 1: I declared it as List<List…> rather than ArrayList. It is usually better to declare such a variable by its interface because that allows you more easily to change implementation.
  • They added 2+1=3 items and I added 2+2=4 to the Lists
  • Yes, that is what I meant it to look like. You can tell by the [[…][…] that you have Lists inside one another. That is what I thought the original question was about.
    The <> without anything written inside is called the diamond operator and only works in Java7.
     
    Campbell Ritchie
    Marshal
    Posts: 80244
    426
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Roberta Fine wrote: . . . a playing card, . . . Please advise on using integers for the rank and suit values. I'm not exactly sure how an enum would work in this case.
    Thanks in advance.


    Nononononono. Don't use ints. The whole idea of enums is to get away from using plain numbers. Read the Java Tutorials, where you find this:

    Java programming language enum types are much more powerful than their counterparts in other languages.

    In the version of the Java Tutorials I used when learning, that sentence ended something like, “…which are merely glorified integers,” but I think there were complaints making them remove that bit
    The idea behind not using plain numbers is to restrict the range. In C/C++/Eiffel (old version) etc., enumerated types are “glorified integers”. The following is valid C code (not Java®).You cannot tell from the output where the enum ends (6, but only the 5 is printed from the enum) and where the ordinary input begins. Now try the same in Java® and you can tell the differenceprintEnum(99); … printEnum(8);
    And you will see the difference in output. You cannot slip 7 in and pretend it is part of the enum.
    If you go back to the card example in the Java Language Specification, it shows that enum elements implicitly implement Comparable<…>, and you can sort them with compareTo(). I think there is even a sort() call in that example.
     
    Roberta Fine
    Greenhorn
    Posts: 27
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is a lot of very helpful information. I am going to look it over in detail and practice making some card decks with some different data structures.
    I'll post again soon with some questions that come from doing that, because you never know until you do it.... Cheers!...
     
    Campbell Ritchie
    Marshal
    Posts: 80244
    426
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You're welcome
     
    I do some of my very best work in water. Like this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic