Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Arrays Of Objects?

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Arrays Of Objects?

    Well I'm not sure how to program this, but anyway, I need to make 81 objects and assign their value (int value).
    So My Class is:
    public class Cell {
    int value;
    int xloc;
    int yloc;
    int possiblevalues[];
    }

    And I need basically 81 objects;
    I really don't want to say
    Cell one = new Cell();
    Cell two = new Cell();
    one.value etc etc
    All my values are stored in a array, so is there any way to efficiently:
    Make 81 objects, assign their values without repeating what I said above?
    I was thinking a for loop and an array of some sort, but I'm not quite sure how to approach this;

    I got this far:
    Not sure what I did but it seems to work?
    public void assignvalues(){
    	GetDataMethods one = new GetDataMethods();
    	Cell[]cellarray; //Declares a Array of the type Cell
    	cellarray = new Cell[81]; // Makes 80 slots, starting with 0
    	for(int x =0;x<=80;x++){
    	System.out.println(cellarray[x]);
     
    		}
    	}
    Last edited by helloworld922; October 20th, 2009 at 08:57 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    5
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Arrays Of Objects?

    You need initialize the objects.

    public void assignvalues(){
    	GetDataMethods one = new GetDataMethods();
    	Cell[] cellarray = new Cell[81]; //Declares a Array of the type Cell
     
    	        for(int x =0; x< 81 ;x++){
                         cellarray[x] = new Cell();
    	              System.out.println(cellarray[x]);
    	       }
    	}

    cellarray[x].someMethod();

    cellarray[x].value;


    Is this what you ment?
    Last edited by bluurr; October 20th, 2009 at 05:41 PM.

  3. The Following User Says Thank You to bluurr For This Useful Post:

    MysticDeath (October 20th, 2009)

  4. #3
    Member
    Join Date
    Jul 2009
    Posts
    31
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Arrays Of Objects?

    Yeah.
    I wasn't sure how to initialize the variable, so thanks :-D.
    I'll post back once I fix this, and I'll post my sol.
    Gah:
    Well you can't see the sol, because you just gave me the sol.
    Thanks.
    Last edited by MysticDeath; October 20th, 2009 at 10:48 PM.

Similar Threads

  1. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 07:29 PM
  2. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 02:38 PM
  3. Java program to encrypt an image using crypto package
    By vikas in forum Java Networking
    Replies: 1
    Last Post: July 7th, 2009, 12:00 PM
  4. Replies: 6
    Last Post: May 15th, 2009, 06:06 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 04:52 AM