• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

objects and arrays

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Felipe Walker
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the information
that worked linke a charm

I made the variable private and used setter and getter methods
works much better
 
Crusading Chameleon likes the size of this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic