aspose file tools*
The moose likes Beginning Java and the fly likes objects and arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "objects and arrays" Watch "objects and arrays" New topic
Author

objects and arrays

Felipe Walker
Ranch Hand

Joined: Sep 23, 2003
Posts: 41
this is my code

public class Event{
public String eventDate;
public String eventName;
public String eventDescription;
public String eventLink;
public String eventContact;
}

public class ObjectArrayTest{


public static void main(String [] args){

Object[] myArray = new Object[3];

Event etest = new Event();
etest.eventDate = "test";
etest.eventName = "test";
etest.eventDescription = "test";
etest.eventLink = "test";
etest.eventContact = "test";

Event etest2 = new Event();
etest.eventDate = "test2";
etest.eventName = "test2";
etest.eventDescription = "test2";
etest.eventLink = "test2";
etest.eventContact = "test2";

myArray[0] = etest;
myArray[1] = etest2;
String thisone = myArray[0].toString();

System.out.println(thisone);

}
}

How do I get seperate variable values from the object in the array
for example I want to get the value of the eventName variable from etest2 I put in the array

Please help
Liam Tiarnach
Ranch Hand

Joined: Aug 06, 2004
Posts: 51
Originally posted by Felipe Walker:
this is my code

public class Event{
public String eventDate;
public String eventName;
public String eventDescription;
public String eventLink;
public String eventContact;
}

public class ObjectArrayTest{


public static void main(String [] args){

Object[] myArray = new Object[3];

Event etest = new Event();
etest.eventDate = "test";
etest.eventName = "test";
etest.eventDescription = "test";
etest.eventLink = "test";
etest.eventContact = "test";

Event etest2 = new Event();
etest.eventDate = "test2";
etest.eventName = "test2";
etest.eventDescription = "test2";
etest.eventLink = "test2";
etest.eventContact = "test2";

myArray[0] = etest;
myArray[1] = etest2;
String thisone = myArray[0].toString();

System.out.println(thisone);

}
}

How do I get seperate variable values from the object in the array
for example I want to get the value of the eventName variable from etest2 I put in the array

Please help

hmm...
a couple things...
since your array hold Object, and you added an Event object ( subclass of Object) to the array you will need to cast it back to Event so that you can access its fields...
i.e...

of course, I did no uses an array element in this example... I will leave that to you to figure out...
although this should be a hint...

myArray[0] and etest variable hold the same reference to you instantiated Event object...


- Liam...<br />- ' He who never sleeps... '
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Hi,

Before I attempt to help you, let me see if I understood your question clearly. You created an object array to store your event objects & at some point of time (code) you wishes to access a member of the event object stored in your array.

In short, you wish to print out all the contents (or some contents) of a specific (or any) event object in your array.

By invoking the toString() method of your event class, you get the hash code of this object rather than the contents. If you wishes to get the contents that this event object contains, you've to override the toString() in your event class.

So what you did below is not exactly correct, but if you'd overrode your toString() to return all its contents, it would have printed everything about the event object.

Originally posted by Felipe Walker:
String thisone = myArray[0].toString();

System.out.println(thisone);


As Liam had pointed out, since you stored your event objects into an Object array, you'll need to class cast them back into the event class. Once you had done that, to access the individual public members of the event class is performed simply by the dot (.) operator.

However, it is generally bad practise to have public member variables. Instead, declare them as private and have corresponding getters & setters method for accessing them.

HTH


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Felipe Walker
Ranch Hand

Joined: Sep 23, 2003
Posts: 41
thanks for the information
that worked linke a charm

I made the variable private and used setter and getter methods
works much better
 
 
subject: objects and arrays