0

I am getting an object array inside a arraylist that is also of Object type. I know within the very first index of the object array list I have a nested object array and I have to retrieve that array. How can I do this? Right now I am iterating over complete object array list which is not good as I know I have to retrieve the object array from the very first object list element.

List<String> resultList = new ArrayList<String>();
List<Object[]> listObj = (List<Object[]>)query.getResultList();
for(Object[] obj: listObj){
    resultList.add(((String)obj[0]));
    resultList.add(((String)obj[1]));
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
wdwd dsd
  • 25
  • 4
  • [`listObj.get(0);`](https://docs.oracle.com/javase/10/docs/api/java/util/List.html#get(int))? – Turing85 Jun 15 '18 at 17:07
  • Are you trying to get all the elements of type as same as the type of the first element? Is this your question? – zlakad Jun 15 '18 at 17:12

2 Answers2

1

You should take a look at this answer because this is exactly what you want: https://stackoverflow.com/a/8882358/9016740

You can access the first element of an ArrayList by using the get(index) method as such:

List<String> resultList = new ArrayList<String>();
List<Object[]> listObj = (List<Object[]>)query.getResultList();
Object[] firstObjArray = listObj.get(0);
resultList.add(((String)firstObjArray[0]));
resultList.add(((String)firstObjArray[1]));
Blake Ordway
  • 308
  • 1
  • 8
-1

Try this.

Object[] resultArr = listObj.get(0);

Now use resultArr to fetch values and store in resultList.