Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 25-11-2009
Member
 
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.
Reply With Quote
  #3  
Old 25-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
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;
		}
	}
}
Reply With Quote
  #4  
Old 25-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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;    
        }
    }
}
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 01:33 PM.