GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes newbie help please with arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "newbie help please with arrays" Watch "newbie help please with arrays" New topic
Author

newbie help please with arrays

Russ Tee
Greenhorn

Joined: Apr 15, 2006
Posts: 6
I am struggling a little with a few ideas and concepts, and so my assignment is getting very behind. I cant seem to connect the right ways of accessing the data of one array from another, ie: importing the data from one to another.
I want too create a copy of one array as another in a different class. Cant seem to find the right syntax, and i am tearing my hair out.

can someone please tell me... pleaaaaaaaaaaaase!

here is what i have so far, and i know it is wrong cos it wont compile:

public class One
{
private int[] one = new int[20];

public One()
{
one = new int[20];
for (i = 0; i < 20; i++)
one[i] = Two[i];
}

{
other important methods here
}
....
}
Trouble is i cant seem to get my head over the hurdle of understanding how to get array one to be the same as existing array two, and without disturbing array two.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi


i can't see where the array "Two" is declared (by the way: in java reference names (of course this is up to you) startLowercaseAndSoOn (unless they are final then they are ALL_UPERCASE)

what do you mean by "beeing the same" ?

do you want "one" referencing the same array as "two" or do you want "one" to be a copy of "two" ?



vs.





a refernce is "just a pointer" (dont hang me for this :-) ) to a piece of memory that forms an object.

if you do

then "one" is pointing to the same memory as "two" is. now if you go on and change the state of the object referenced by "two" (e.g. changing the value at a specified index



then this change is also reflected by the reference "one" as it is referencing the same object...

if you copy the array, then you allocate a new chunk of memory (referencing it by "one") and make it having the same state as array "two". if you change either of the arrays, the changes are not refelected in the other.


pascal
[ April 15, 2006: Message edited by: pascal betz ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Welcome to the Ranch, Russ.

You haven't sent us the whole of your code, but I can see one thing already.
You appear to be initialising the one[] array twice.
Start off with

Also declare i in the "for" loop.
And don't use "20," use arrayOne.length.
Don't call the arrays just one and two; always use identifier names which make it obvious what you are dealing with.

If your arrays are arrays of primitive types, you can set arrayOne the same as arrayTwo without disturbing arrayTwo's members, because you are passing the values.
If, however, your arrays contain objects, then you pass the reference with the = operator, so the two arrays contain references to the same objects, and altering an object in arrayOne will also alter it in arrayTwo.

I hope that has been of some help to you.
CR
Russ Tee
Greenhorn

Joined: Apr 15, 2006
Posts: 6
Thanks for replying.
The assignment is to get a game of Solitaire working. Already given some of the working parts, just need to construct a few classes and methods. It is just getting my brain haywire.
Here is the original code of array Two (written by Course instructor):

/**
* A full deck of cards. It is shuffled when first constructed
* and thereafter cards are removed from the deck by calls to
* nextCard until no further cards remain. Cards can also be
* placed back on the deck at either the start or the end.
*/
public class Deck
{
private java.util.List<Card> cards; // The list of cards
private static java.util.Random generator = null;

/**
* Constructor for a shuffled deck of cards.
* @param seed the seed for the random number generator used to
* shuffle the deck. A different unique shuffle is obtained for
* each possible value of seed.
*/
public Deck(long seed)
{
// Create a deck of cards in sorted order of suit then pip count.

cards = new ArrayList<Card>();
for (int suit = Card.SPADES; suit <= Card.CLUBS; suit++) {
for (int pips = 1; pips <= Card.CARDS_IN_SUIT; pips++) {
cards.add(new Card(suit, pips));
}
}

// Now shuffle the deck, using a random number generator based
// on the seed value given as a parameter.

java.util.Random generator = new java.util.Random(seed);
java.util.Collections.shuffle(cards, generator);
}
and some other methods of deck...
}

There is another class Card which represents a deck of playing cards, ie constructs them given suits and pips.
What I am trying to do is the following:
Create a new class called Stock which can use the class Deck and thereby the cards, but I am not allowed to change the code of Deck. The stock is the rest of the cards that you play the game with.
so far...

public class Stock
{
private int[] stock = new int[Deck.length];

/**
* Constructor for objects of class Stock
*/
{

stock = new int[Deck.length];
for (int i = 0; i < Deck.length; i++)
stock[i] = Deck[i];
}

/**
* Method which views the top card of the stock
* and returns the value of the top card
*
*/
public int top()
{
return stock[0];
}

/**
* Other methods here
*/
}

For a start, it returns an error message saying it cannot find symbol - variable length. I haveto be able to use a deck in my methods to make the game work.
What I need please is a little help getting my head around the syntax for doing the above. Obviously I want to learn to code myself, but until I can understand how to do what is a simple thing as above, i cannot go further. I know what I want to do in English, its just the translation into Java that hurts
Thanks again for your help so far.
Russ Tee
Greenhorn

Joined: Apr 15, 2006
Posts: 6
oops now i see how the "code" button works
aymane chetibi
Ranch Hand

Joined: Apr 12, 2006
Posts: 175
what do you mean when you do: Deck.length ??
Is Deck the class itself or do you mean an object of type class Deck ?
If you mean an Object of type Deck than you have to instantiate it first, and you can do that in the constructor.
Also Don't initialize stock twice...
do all your initialization in the constructor.
and you can for example let the constructor take a Deck object as a parameter and then intialize the stock array to be the same as Deck.

public stock(int[] Deck){
stock = new int[Deck.length];
for(int i=0; i<Deck.length; i++) stock[i]=Deck[i];
}
Russ Tee
Greenhorn

Joined: Apr 15, 2006
Posts: 6
Thanks again for replying.
The Deck-class array has a length of 52.
Yes I do mean an object of type class Deck.
I realise now that I had initialised stock twice.
The Deck itself is a class and it has a field called cards.
So I want to make stock instantiate a new Deck class object called cards, or have it point to the same array that Deck uses.

I have tried with the bit of code you wrote, but it returns error cannot find symbol - variable length. So obviously it is not refering to an array.
I am still confused.
Russ Tee
Greenhorn

Joined: Apr 15, 2006
Posts: 6
I cant belive its taken me this long to figure it out.
All along I just needed to write Deck deck = new Deck(52);
Thanks for your help. I have just gotten over a mental hurdle.
Russ Tee
Greenhorn

Joined: Apr 15, 2006
Posts: 6
Just for the scholars, my way of doing what i wanted was:


hope anyone reading all the above can now see what i was asking all along, thanks
 
GeeCON Prague 2014
 
subject: newbie help please with arrays