Results 1 to 4 of 4
  1. #1
    riz618 is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default Need help with creating array of type object

    Hi All,

    I dont very well understand how exactly do i create a array of type object. Below i will paste what i am trying to do, so that you have a good idea of what i am talking about.

    Thanks
    Java Code:
    package Person;
    
    public class PersonTest 
    {
    	static Person[] p;
    	
    	public PersonTest()
    	{
    		p=new Person[3];
    	}
    	
    	public void insert()
    	{
    		for(int i=0;i<(p.length-1);i++)
    		{
    			p[i].name="i";
    			p[i].ID=i;
    			
    		}
    	}
    	public void display()
    	{for(int i=0;i<(p.length-1);i++)
    	{
    		System.out.println(p[i].name);
    		System.out.println(p[i].ID);
    		
    	}
    		
    	}
    
    
    	public static void main(String[] args)
    	{
    		PersonTest t = new PersonTest();
    		t.insert();
    		t.display();
    
    	}
    
    }
    Java Code:
    package Person;
    
    public class Person
    {
    	String name;
    	int ID;
    	public Person()
    	{
    		
    	}
    
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    Java Code:
    // Each element in this array is null:
    p=new Person[3];
    ...
    // Instantiate array element "i":
    p[i] = new Person();
    p[i].name="i";
    p[i].ID=i;

  3. #3
    riz618 is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    Based on what you said i have done it and my new code is as under, still it says method Insert not defined for class PersonTest

    package Person;

    public class PersonTest
    {
    static Person[] p;

    public PersonTest()
    {
    p=new Person[3];


    public void insert()
    {
    for(int i=0;i<p.length-1;i++)
    {
    p[i]=new Person();
    }
    }
    for(int i=0;i<(p.length-1);i++)
    {
    p[i].name="i";
    p[i].ID=i;

    }
    }
    public void display()
    {for(int i=0;i<(p.length-1);i++)
    {
    System.out.println(p[i].name);
    System.out.println(p[i].ID);

    }

    }


    public static void main(String[] args)
    {
    PersonTest t = new PersonTest();
    t.insert();
    t.display();

    }

    }

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    You put the insert method inside the constructor. This is not allowed.
    So change this:
    Java Code:
    public PersonTest()
    {
        p=new Person[3];
    
        public void insert()
        {
            for(int i=0;i<p.length-1;i++)
            {
                p[i]=new Person();
            }
        }
    
        for(int i=0;i<(p.length-1);i++)
        {
            p[i].name="i";
            p[i].ID=i;
        }
    }
    to this:
    Java Code:
    public PersonTest()
    {
        p=new Person[3];
    }
    
    public void insert()
    {
        for(int i=0;i<p.length-1;i++)
        {
            p[i]=new Person();
        }
    
        for(int i=0;i<(p.length-1);i++)
        {
            p[i].name="i";
            p[i].ID=i;
        }
    }

Similar Threads

  1. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 PM
  2. List views, a type of object
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 04:07 AM
  3. Getting object type
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:49 AM
  4. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 02:04 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •