How do we get List objects in the opposite direction?

Hi, I get a List object containing pojo class objects in a table. in my case, I have to show the table data in reverse order. means that for I add some rows to a specific table in the database, when I added recently, the data is stored in the last row in the table (in the database). here I have to show all the contents of the table on my jsp page in reverse order, meaning that what I inserted recently should display the first row on my jsp page.

here my code was like

List lst = tabledate.getAllData();//return List<Table> Object
Iterator it = lst.iterator();
MyTable mt = new MyTable();//pojo class
while(it.hasNext())
{
     mt=(MyTable)it.next();
     //getting data from getters.
     System.out.println(mt.getxxx());
     System.out.println(mt.getxxx());
     System.out.println(mt.getxxx());
     System.out.println(mt.getxxx());
}
+3
source share
2 answers

In this case, you cannot use an iterator. You will need to use index-based access:

int size = lst.size ();
for (int i=size - 1; i >= 0; i --)
{
  MyTable mt = (MyTable)lst.get(i);
  ....
}

Btw: MyTable() . , .

+1

ListIterator, hasPrevious() previous():

ListIterator it = lst.listIterator(lst.size());
while(it.hasPrevious()) {
    System.out.println(it.previous());
}
+9

Source: https://habr.com/ru/post/1782988/


All Articles