Reputation: 85
Suppose we create an listOfObjects
of type ArrayList
, which contains objects of type Object
:
public static ArrayList listOfObjects = new ArrayList();
We have an Object
constructor, which creates an Object
with two fields, name and type:
public Object(String name, int type) { /*some code*/ }
Now we create two Object
s, then add them to listOfObjects
:
public static Object object1 = new Object("object one", 1);
public static Object object2 = new Object("object two", 2);
listOfObjects.add(object1);
listOfObjects.add(object2);
Assuming that object1
and object2
were correctly added to listOfObjects
, how can we access the type
field of an object in the list?
I have tried
listOfObjects.get(1).type;
but that doesn't seem to work. Is there a way to do this?
EDIT Object
is an example name.
Upvotes: 3
Views: 8673
Reputation: 1
This is old, but none of the given solutions are sufficient. If you want to access the fields and methods of your object of type "Object" which you've inserted in the ArrayList "listOfObjects", you have to cast it. In your case this would work:
((Object) listOfObjects.get(1)).type;
This would return, based on your example, the object2.type;
Not to get confused, if your object's class was called "Car", it would be ((Car) listOfObjects.get(1)).type
[note] Its important that your object's field or method you are trying to access (in this case the "type" field) is outside of the parentheses surrounding (Object) listOfObjects.get(1)
, otherwise the above code will throw an error.
"Object" is truly unfortunate naming for a class.
Upvotes: 0
Reputation: 13816
Object
, since there is already a class called java.lang.Object
, and since it's in the java.lang
package, all your sources implicitly import it.public int type;
, if you want to access it the way, you posted.public int getType() { return type; }
Upvotes: 4
Reputation: 32175
First of all Change the name of your class to something else but Object
, then in this class declare two fields name
and type
where you will store the values you've passed in your constructor, your code should be like this:
public class TestObject {
//The best approach is to declare your fields here as private
private String name;
private int type;
//default non parameterized constructor
public TestObject() {
super();
}
public TestObject(String name, int type) {
this.name = name;
this.type = type;
}
//getters and setters of your fields to make them accessible in public
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public int getType(){
return this.type;
}
public void setType(int type){
this.type = type;
}
And to get an object type
from your list, your call should be like this:
//use getType() getter to get the type field value
listOfObjects.get(1).getType();
Upvotes: 0
Reputation: 1385
Your Object class should be like,
public class Object {
private String name;
private int type;
public Object(String name, int type) {
this.name = name;
this.type = type;
}
public int getType(){
return type;
}
}
Then you can reach by listOfObjects.get(1).getType();
Upvotes: 1
Reputation: 16152
First of all, use something else as a class name than Object
. While you can use it, it will just add confusion to the discussion. (Hint: that's why metasyntactic variables such as foo and bar are used).
Secondly, you say "it does not work". In what way? Does the program compile? Does it run? Do you have an error message?
Lastly, read up on Java Generics, they're like 16 year old by now. If you're a student and you get taught collections without generics, educate your professor (or you're getting ripped off).
Finally, rewrite this line:
public static ArrayList<Object> listOfObjects = new ArrayList<>();
Upvotes: 0
Reputation: 3373
Your constructor public Object(String name, int type)
should assign the arguments to public object variables. Example:
public class MyObject {
public String name;
public int type;
public MyObject(String name, int type) {
this.name = name;
this.type = type;
}
}
Then the call listOfObjects.get(1).type;
will work.
Edit: renamed class from Object
to MyObject
.
Upvotes: 1