Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-29-2008, 03:27 AM
Member
 
Join Date: Jan 2008
Posts: 8
riz618 is on a distinguished road
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
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(); } }
Code:
package Person; public class Person { String name; int ID; public Person() { } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-29-2008, 03:52 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
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;
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-29-2008, 04:00 AM
Member
 
Join Date: Jan 2008
Posts: 8
riz618 is on a distinguished road
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();

}

}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-29-2008, 07:14 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
You put the insert method inside the constructor. This is not allowed.
So change this:
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:
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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using reflection to check array type and length Java Tip java.lang 0 04-14-2008 09:42 PM
List views, a type of object Leprechaun New To Java 2 02-06-2008 04:07 AM
Getting object type gapper New To Java 1 01-20-2008 09:49 AM
How to cast an Object into a specific type (Integer/String) at runtime mailtogagan@gmail.com Advanced Java 2 12-03-2007 02:04 PM
Creating object of Type Object class venkatv New To Java 3 07-17-2007 04:33 PM


All times are GMT +3. The time now is 06:51 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org