简体   繁体   中英

accessing an object array item, java

I have created a number objects using an array statement, and I can println the values passed within the class as it is created, but when I try and retrieve element values from outside of the class (monopolygame class) it doesn't recognise the refrence - how can I refrence this correctly?

public class monopolygame {

    public static void main(String[] args) {

        //set up array of 18 objects    
        property properties[] = new property[18];

        //create 18 property objects and populate array
        properties[0] = new property("a","available",400,500);//create property
        properties[1] = new property("b","available",400,500);//create property 
        properties[2] = new property("c","available",200,300);//create property 
        properties[3] = new property("d","available",100,180);//create property
        properties[4] = new property("e","available",400,700);//create property
    }
}

property class...

public class property
{
   public static void main(String[] args)
   {
   }

   //constructor
   public property(String propertyname, String owner, double price, double rent) 
   {
        System.out.println("Property info for " + propertyname 
                           + " - Rent : £" + rent 
                           + "Price : £" + price 
                           + "owned by :" + owner);
    }   
}

I am using this kind of reference in the monopoly class to try and access the data

if (properties[2].propertyname == "available")
{
    System.out.println("avaialble");
}
else
{
    System.out.println("sold");
}

Thanks

You have to declare those attributes in the "property" class first:

class property  {
    String propertyname;
    String owner;
    int    price;
    int rent;
     public Property( String somename, String owner, int price, int rent ) {
         this.propertyname = somename;
         this.owner = owner;
         this.price = price;
         this.rent = rent;
         // and so on
      }
 }

The array you're using is local to the main method.

To access it outside of the scope of the main method you should declared either as a class attribute or as an instance attribute like this:

public class monopolygame {
    public static property properties[]; 
    public static void main(String[] args) {
    //set up array of 18 objects

    properties = new property[18];
    .....

That way you can access the array in other method like this:

    public void setUp() {
         for( property p : properties ) {
             System.out.println( p.propertyname ); // etc. 

Then your code:

if (properties[2].propertyname == "available")

Will work.

BTW in Java all the class name start with uppercase by convention , so it should be:

Property instead of property and MonopolyGame instead of monopolygame

Given the code you've supplied us with, it doesn't look like you're actually storing the values passed in to your property constructor. Here's something a bit closer to what your property class should look like:

public class property
{
    private String propertyname;
    private String owner;
    private double price;
    private double rent;

    public String getPropertyName()
    {
        return propertyname;
    }

    public void setPropertyName(string newName)
    {
        propertyname = newName;
    }

    // more getter/setter methods here

    public property(String propertyname, String owner, double price, double rent)//constructor
    {
        this.propertyname = propertyname;
        this.owner = owner;
        this.price = price;
        this.rent = rent;

        System.out.println("Property info for " + propertyname + " - Rent : £" + rent + "Price : £" + price + "owned by :" + owner);

    }
}

A few remarks:

  • In Java, string comparisons need to be done with the equals() method, not ==. See this link for an explanation of why using == might work in some cases, but that shouldn't be expected.

  • It is a convention to capitalize class names -> Property rather than property.

  • Avoid mixing and matching bracket positioning. Use at the end of the same line or at the beginning of the next line, but not both. The most frequent use is at the end of the same line.

You need to add a public method to access the internal of monopolygame class. That is the main aspect of your question.

But in general your code is not the correct way of doing things in Java. Class names must be capitalized, for example. An empty main in the second class is pointless. You need to learn more about the basic Java stuff, I answer your question because I think you could learn a lot here, but I suggest you to check the trails covering the basics on The Java Tutorial .

Two problems are immediately obvious:

  • You're not storing the arguments passed to the property constructor in fields within that class.
  • Once you do that, you're trying to compare strings by reference (or by identity , via == ) rather than by value (via String#equals(String) ). Unless you've interned the strings via String#intern() , two different String instances with the same character content will not compare as equal via == . That comparison only looks at the memory addresses of the object references, which will most likely point to two different String instances, each with a different address.

As this question looks like a homework assignment, please tag it as such.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM