Reputation: 3
this might be a silly question, but due to my lack of knowledge on Java(still a beginner on it) I need some help to read the elements of an Object. There is a method that is returning not only the Entity but also a String and I cannot read/iterate it. I've tried some solutions from the internet but none of them worked as I need. I might be doing something wrong, so once again sorry if this is a silly question.
Let me share exactly what the method is returning here
As you can see, I have ChecklistRequest on position 0 and a String ("--") on the position 1. I need to read both of them as I need to manipulate both after this point.
Thanks in advance!
Upvotes: 0
Views: 2720
Reputation: 13195
What you see is that storage is an ArrayList<Object[]>
internally. And while you can always set an Object
to any kind of other object, if you want to read, it, that will need explicit casting:
Integer i1=1; // upper case Integer (not int), so it is an object
Object o1=i1; // setting Object to any object is okay
//Integer i2=o1; // this will not compile
Integer i2=(Integer)o1; // this will work fine
if(o1 instanceof Integer)
System.out.println("o1 is Integer: "+(Integer)o1*2);
if(o1 instanceof Double)
System.out.println("o1 is Double: "+(Double)o1*2);
System.out.println("passed the ifs, let's die");
Double d1=(Double)o1; // this will result in ClassCastException in runtime
(https://ideone.com/qdGtJP)
This is why you could not see/do operations on the result of request.get(0)[0]
, because that was still an Object
, not a String
or a CheckListRequest
. It would need specific casting (note that there is an instanceof
operator in Java, so you can check if something is really what you think it is - because when it is something else, the casting attempt will die)
The E
-s are just stand there for ***E***lement, you can see that everywhere in the documentation, like https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/List.html:
Module java.base
Package java.util
Interface List<E>Type Parameters:
E - the type of elements in this list
When running, Java does not really track what you specify for a container type to contain, it will put anything into a List
object, and then die showing a suprised face:
List<Integer> il=new ArrayList<>();
//il.add(new Object()); // this would not compile
List l=il;
l.add(new Object()); // this compiles, and runs, so add() does not complain
System.out.println("Length: "+il.size());
System.out.println(il.get(0)+3);
specifically with a ClassCastException
again, so List<Integer>
internally knows that it is just storing objects, and only the compiler generates a convenient casting for us, so we can live pretending that the List<Integer>
has Integer
s inside. But it does not.
Side remark1: this is why List<int>
does not exist: it needs objects
Side remark2: in case of a field, it is possible to get its "type parameters" (the <E>
) via reflection.
public class Test {
public static class CheckListRequest {}
public static void main(String[] args) {
List<Object[]> array=new ArrayList<>();
array.add(new Object[] {new CheckListRequest(),"--"});
array.add(new Object[2]);
System.out.println("Test");
// would not compile, Object has no length()
//System.out.println(array.get(0)[1].length());
// compiles and runs just fine
System.out.println(((String)array.get(0)[1]).length());
}
}
If you put a breakpoint for example at the Test
-line, you can see a very similar structure in the debugger.
Upvotes: 0
Reputation: 417
To get a specific element of an object, you can use format "objectName.certainElement"
A lot of the time however, the actual variables are private, so you can't access them from outside the class itself. The class has to provide "getters and setters", and you would call objectName.getElement() in order to get that particular element.
In your case, requests is a DistinctResultList full of E's. My bet is that the DistinctResultList is just a wrapper for ArrayList. In order to get the object you are looking for, you must do something like this.
checklistRequest = requests.get(0)[0]; //Get the first object at position 0 of arraylist, then the index at position 0 of that object.
secondString = requests.get(0)[1];
To edit the values of them, assuming they are public,
request.get(0)[0] = otherChecklistRequest;
request.get(0)[1] = "---";
Or, create a new object and set the index altogether:
request.set(0, new myObject(otherChecklistRequest, "----"));
Of course you would have to modify all of these to fit your use case.
Different objects have different ways of getting the elements hidden within. In this case, ArrayList uses a getter method with an index as the argument. The object inside seems to be just an array which uses brackets.
Best of Luck, I hope I was able to help, or at least point you in the right direction!
Upvotes: 1