• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays questions  RSS feed

 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you remove items from an array once they are used or picked? I know there is a Shuffle function which I haven't looked into yet. I wanted an array without duplicates (either words or numbers) and I am just starting to research that.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A collection without duplicates... maybe an array isn't the right thing to use. In mathematics, a collection without duplicates is called a set. Maybe there's something like that you could use...

(Hint: there is)
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I was thinking about an array of words - 10 for example. I want to grab a random number of the ten and use that word, but I don't want to use that word again once that number (or position ?) has been picked.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is different. That is not random, but random selection from a declining population.
You can yes.
I shall let you work out how to do it. The principle is not difficult.
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A friend showed me this and I retyped it from a picture I took of his writing. I understood it at the time, but now I am lost again (and there is an error). He told me to randomize the values at the position instead of trying to randomize the array. This way I could just go in order c++ for the array and the numbers in the positions would be random and I wouldn't have to worry about a number being "Removed" from the deck.
I have two methods, the first one doesn't give me errors. It is just a way of putting numbers in the array or "building" the deck (I know there are only 3 values)
The second method is swapping, which I am trying to get my head around, as well as the way the method/arrays are laid out in the public static etc. part.

Here is the error with the second method . This might be an easy fix and I am still trying to figure it all out.

"Multiple markers at this line
- Line breakpoint:RandomTest1 [line: 7] - main(String[])
- The method shuffleDeck(int[]) in the type RandomTest1 is not applicable for the arguments () "


 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

What reminds you "pass by value", "pass by reference"?
In a method "shuffleDeck" you're doing something what is not really necessary, question is what?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what you mean. Pass by Value and Pass by reference mean nothing to me, even after looking them up.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:"Multiple markers at this line
- Line breakpoint:RandomTest1 [line: 7] - main(String[])
- The method shuffleDeck(int[]) in the type RandomTest1 is not applicable for the arguments () "

Do you understand what that error message means?

It means that the method shuffleDeck takes an int[] as an argument, but you are trying to call it without any arguments. You have to pass it an int array.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See if it helps to notice the difference by passing Object reference as an argument, and primitive value. Try to analyse and understand. Tell what comes to your mind.Output:
a: 4
b: 8
A: 4
B: 4

Output:
A: 4
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "argument" mean? I think I am getting around passing it as an int array. I really need a tutor.

My friend gave me the correction for this problem, but I still have to figure out why it worked:
He called the method using this instead

int [] deck = makeDeck();
deck = shuffleDeck(deck);

The first "int [] deck = makeDeck();" looks weird because I am used to calling methods with just the open brackets . I am not sure why he had to make it a int[] deck = for it
The second deck = shuffleDeck(deck), calls the second method, but I don't know why he has put "deck =" on it.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16058
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David De wrote:What does "argument" mean?

An argument is a value that you pass to a method when you call the method. It's what you put between the ( and ) when you call a method.

See: Passing Information to a Method or a Constructor in Oracle's Java Tutorials.

David De wrote:The first "int [] deck = makeDeck();" looks weird because I am used to calling methods with just the open brackets . I am not sure why he had to make it a int[] deck = for it

It declaring a variable (<= click that link) named 'deck' of type 'int[]' (int array); then the method 'makeDeck' is called and its return value (<= click that link) is assigned to the variable.

David De wrote:The second deck = shuffleDeck(deck), calls the second method, but I don't know why he has put "deck =" on it.

Same thing, you're calling the 'shuffleDeck' method, and you pass it the variable 'deck' as an argument, and then you assign the return value of the method to the variable 'deck'.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
David De wrote:The second deck = shuffleDeck(deck), calls the second method, but I don't know why he has put "deck =" on it.

Same thing, you're calling the 'shuffleDeck' method, and you pass it the variable 'deck' as an argument, and then you assign the return value of the method to the variable 'deck'.

David De, if it is done in this way, probably would be better to name a reference variable to: "shuffledDeck" (so you'd know deck is shuffled after the method is called), otherwise, there is no point to return anything in the method "shuffleDeck", because it has an impact to an original array, since you passing to method a reference to array, not the array itself. Check my post above, so you'd see the difference in outputs whats happening when you pass copy of primitive variable and reference to an object (check how changes/not changes original their values).
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So maybe we should back up a bit. methods are chunks of code that do stuff. they should be rather small, and well-named, so that by reading the name you know (or at least have a pretty good idea of) what the method does.

You have a method called "shuffleDeck". Based on that name, I assume that it would shuffle a deck of cards. But to make it useful, it should be able to shuffle ANY deck of cards you want it to. So when you say "hey, java...shuffle a deck of cards", you have to tell it WHICH deck. You do that by passing in an argument. You say "hey java, shuffle THIS deck of cards".

That's what he did with this snip of code:

shuffleDeck(deck);

He told java to shuffle the cards in the array "deck".

Now, I don't know how exactly the shuffleDeck method works, but based on the code you provided, it is going to return an array that is a shuffled version of the array you gave it. When it returns that, we need to keep hold of it. Since we don't need our old, unshuffled deck any more, we change our "deck" variable to point to this new version...that's what the "deck =..." means on this line:

deck = shuffleDeck(deck);

You can have a method that takes zero, one, two...seven, thirty...or even an unlimited number of parameters (although there are design considerations to consider...a method with thirty parameters is probably not good). You pass stuff in (called arguments) to a method. inside the method, you refer to them as parameters.

Does that clear up (at least) some of your questions? If not, what can we elaborate on?
 
Patrick De
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, that was the best explanation for me I could ask for. Clear and concise. I know that sometimes it takes a really dumb down approach to get me to understand, in that case, you spelled it out perfectly for the second method. Thank you.
And this is not to take away from the other posters' generous advise and responses I appreciate those too but I am just not there yet...

Now, that you have explained it to me, I understand the second method, BUT

1) how about the first calling of that method?

int [] deck = makeDeck();

why must there be the "int [] deck =" in front? What can't it just be makeDeck()?

2) And the actually method itself " public static int[] makeDeck()"

Does it mean that any time there is an array in the method, you need to make the METHOD an int []?

 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method, as well as taking arguments, a method can return a value to the calling code.
You can have as many arguments as you like, but you can only return ONE thing.

The general layout of a method is:

AccessModifier returnType nameOfMethod(parameters)

In this case you have the method:
public int[] makeDeck()

Access Modifier: The method is public, anyone can call it
Return Type: This method returns an array of integers.
name of method: makeDeck
parameters: no parameters to this method.


So what does this method do?
Judging from the name it "makes" a deck of card and returns it the deck to your calling program.
If you just call makeDeck() it makes the deck of cards, but then throws it away because noone is around to grab it.

Because you don't want to throw away the deck of cards the helpful method has just made for you you need to store it in a variable.
Which is what the bit: int[] deck = is for.
It declares a variable called "deck" and stores the result of calling "makeDeck" into that variable .

It could be broken down to

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!