开发者

make a copy of list

I need to compare data retrieved from table and a list :

      // first click

      // get list of data
      originalList = getValue(columnX);


      // copy
      List<String> copy1 = new ArrayList<String>(originalList );

      //sort
      Collections.sort(copy1);
      System.out.println("copy" + copy1);

    // verify
     assertEquals(copy1, original开发者_如何学运维List);  //this is OK

    //second click is done here

    // copy
    List<String> copy2 = new ArrayList<String>(originalList ); 


  // sort
  Collections.sort(copy2 );
  System.out.println("copy2" + copy2 );=>copy2 give same values as copy1!!!it should  not

// verify assertEquals(copy2, originalList); =>wrong


I guess that the first assertEquals succeeded because the data in originalList was already sorted. Sorting copy1 again did not change the order, hence equals returns true.

The second click did change the order (I assume that the list is now in reverse order). Hence the second assertEquals fails - originalList and copy2 do contain the same elements but in different order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜