List a specific element in the first place

Artificial intelligence, zero-based entry! http://www.captainbed.net/inner

Sometimes there will be such a demand, that is, the list found from the database needs to be processed, such as a list of department members. The product requires you to rank the department manager first. At this time, you can use the built-in tool under the collection tool category. Methods, practices:

You need to traverse the collection to find the position of the element in the collection , and then use the Collections.swap(list,o,i) (O: is the current location of the element, i: is the location to be placed) method to perform the element swap

public static void main(String[] args) {
       List<String> list = Lists.newArrayList();
       list.add("test1");
       list.add("test2");
       list.add("test3");

       System.out.println("更改前:" + JSON.toJSONString(list));

       Collections.swap(list, 1, 0);

       System.out.println("更改后:" + JSON.toJSONString(list));
}

 

Guess you like

Origin blog.csdn.net/qq_35860138/article/details/103833253