Results 1 to 4 of 4

Thread: Creating an array from a class

  1. #1
    Join Date
    Jul 2009
    Posts
    188

    Creating an array from a class

    Hi
    I want to create an array by declaring a class, instead of a variable type int double etc. I have created a program. I have posted it below. I'm trying to set up an array with class Ply here, but the java compiler is giving any error message. I can not figure out the error. Can any one help with this.
    Code:
    public class MainClass
    {
        public static void main()
        {
            Scanner in = new Scanner(System.in);
            Ply[] myTeam = new Ply[11];
            
            for(int i = 0; i < myTeam.length; i++)
            {
                myTeam[i] = i + 1;    
                Ply[] myTeam = {1,2,3,4,5,6,7,8,9,10,11};
            }
        }
    }
    Last edited by Kingfisher; 25-11-2009 at 02:45 PM.

  2. #2
    Join Date
    Nov 2009
    Posts
    446

    Re: Creating an array from a class

    Hi
    I think there is a problem with this line of the program.
    Code:
    Ply[] myTeam = {1,2,3,4,5,6,7,8,9,10,11};
    i think here the line is trying to initialize your array of players with integers. I think you should initialize it with Player objects instead.

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Creating an array from a class

    Hi
    I am posting a code which creates the array of the player class and sets the players with age and name. Just go through the code below.
    Code:
    public class MainClass {
    
    	public static void main(String[] args) {
    		
    		Ply[] myTeam = new Ply[11];
    		
    		for (int i = 0; i < myTeam.length; i++) {
    			Ply Ply = new Ply();
    			Ply.setName("name");
    			Ply.setAge(23);
    			
    			myTeam[i] = Ply;
    		}
    	}
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Creating an array from a class

    Hi
    I think you are dealing with the arrays more complicatedly. There is a proper way of manipulating arrays. Using array is simple. I have posted a simple code on array. This will definitely help you.
    Code:
    public class MainClass
    {
        public static void main()
        {
            Scanner in = new Scanner(System.in);
            int[] myTeam = new int[11];
            
            for(int i = 0; i < myTeam.length; i++)
            {
                myTeam[i] = i + 1;    
            }
        }
    }

Similar Threads

  1. Creating a generic class in dot net
    By Vedic in forum Software Development
    Replies: 4
    Last Post: 16-02-2011, 08:51 AM
  2. Creating a vector type of a class
    By Xmen in forum Software Development
    Replies: 5
    Last Post: 24-02-2010, 03:38 AM
  3. Creating array with Bash shell
    By Carnie in forum Operating Systems
    Replies: 5
    Last Post: 19-02-2010, 07:54 PM
  4. How an Array Class is Used in Javascript?
    By PsYcHo 1 in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 12:04 AM
  5. Problem in creating array from string values
    By Cedric in forum Software Development
    Replies: 4
    Last Post: 19-01-2010, 05:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,470,735.40351 seconds with 16 queries