Results 1 to 4 of 4
  1. #1
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Can I store / manipulate a 2d object as an array?

    Edit: Wow, that title is all wrong. Can I store / manipulate a 2d array as an object? is how it should read, sorry!
    Right now I'm making a program that uses the following array:
    Java Code:
    char[][] arrayPLAYER= new char[10][10];
    for (i=0; i<arrayPLAYER.length; i++){  // i is vertical, 
    for (j=0;j<arrayPLAYER[i].length; j++){ // j is Horizontal
    arrayPLAYER[i][j]='0';//
    }
    }
    I'm wondering if I can take this array and store it as an object then apply certain bool-style flags to it:

    Example:
    Java Code:
    if (arrayPLAYER.used == 1){
    ..
       System.out.println("That spot has been used already choose again");
    //Choosing algorithm here
    ..
    }
    else{
    arrayPLAYER.used[SelectedBoat.location[0]][SelectedBoat.location[1]]=1;
    arrayPLAYER.select[SelectedBoat.location[0]][SelectedBoat.location[1]]=SelectedBoat.Letter // adds the selected boats letter to the array location.
    //whatever else
    }
    etc. etc.

    I'm just a bit confused about how I can store / manipulate something like a 2d array and would really appreciate some help on better understanding this concept,

    Thanks in advance!
    Last edited by sarevok9; 04-19-2011 at 07:46 PM. Reason: First line.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    I think what you want is a new class to hold the array and other information associated with it, with the ability to process it. Something like this should do:

    Java Code:
    public class Players {
        private char[][] players;
        private boolean used;
        Players(char[][] playerArray) {
            players = playerArray;
        }
        public boolean isUsed() {
            return used;
        }
        public void addBoat(int x, int y, char boatLetter) {
            players[x][y] = boatLetter;
        }
    }

    To use the class, instantiate the class with your data:
    Java Code:
    Players myPlayers = new Players(arrayPLAYER);
    //add boat
    myPlayers.addBoat(0, 1, SelectedBoat.Letter);

  3. #3
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    I'm still a little bit confused on this. Would I just declare an array in main() or would I just be using the use that's in the class without doing any editing to it at all?

    Also: I've always been a bit confused about using private members, public are simple (C-like in their usage, which is my primary language) but private just confuse the daylights out of me, can you explain this a little further for me please?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    You would have to create your array as normal, then create the object from the array
    Players myPlayers = new Players(arrayPLAYER);

    If you don't declare it in "main" you'll need to call the method that declares it in "main".

    From then onwards, you should use your 'myPlayers' object to do all the manipulation.

    Regarding the 'private' keyword, read this:
    Controlling Access to Members of a Class (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Similar Threads

  1. HOw to Store Data in An Array
    By deshmukh.niraj04 in forum New To Java
    Replies: 4
    Last Post: 04-06-2011, 04:00 AM
  2. How to Store ArrayList into an Object?
    By TheGame in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 10:38 PM
  3. store data into an array from a file
    By chopo1980 in forum New To Java
    Replies: 10
    Last Post: 11-13-2009, 09:48 AM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. Store textfile data in an array
    By mokonji in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 05:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •