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 Class

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    9
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Arrays of Class

    I am trying to create and array of a particular class, Ant, its actually an ArrayList and a multidimensional Array, see below. What I need to know is what do I need to add or change to do this.

    Game.java
    	public void initializeBoard()
    	{
    		ArrayList board[][] = new ArrayList[27][27];
    	}
    Ant.java
    public class Ant {
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Arrays of Class

    create and array of a particular class
    You create an array by using the [] notation with the data type:
    TheClassName[] anArrayOfTheClassName; // define array of TheClassName objects
    anArrayOfTheClassName = new TheClassName[5]; // assign a value to the array
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2024
    Posts
    75
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Arrays of Class

    In this code:

    You can create a new class called Board to manage the board functionality.
    Within the Board class, we declare a 2D ArrayList array called board to hold Ant objects.
    In the initializeBoard method, we initialize the board array and then populate it with new ArrayLists of Ant objects for each cell in the grid.

    If you ever get stuck with your programming homework or need some extra help, you might want to check out https://www.programminghomeworkhelp.com/.

Similar Threads

  1. Copying arrays from one class to another
    By hannah87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 8th, 2024, 03:52 AM
  2. [SOLVED] Class of arrays
    By Vessel06 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 9th, 2013, 08:46 PM
  3. Do Class Casts Work With Arrays?
    By snowguy13 in forum Collections and Generics
    Replies: 8
    Last Post: February 23rd, 2012, 08:18 AM
  4. [SOLVED] Arrays being initialized from another class
    By Java Man 9000 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 11th, 2012, 04:34 PM
  5. Issue with setting up different class with arrays
    By D-COD3R in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 06:09 PM