小编4ul*_*lls的帖子

尝试使用比较器对 ArrayList 进行排序

在下面的代码中,该对象someObj有两个属性: afloat x和 an int pnt。AnArrayList<someObj>被创建,然后Comparator根据 x 使用接口进行排序。该属性pnt旨在跟踪排序后的元素。

我从https://www.geeksforgeeks.org/collections-sort-java-examples/复制了代码 ,并对其进行了一些修改以满足我的需求。我想知道可能出了什么问题,它只是没有完成排序工作。

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Random;

public class ArrayListSorting {

   public static void main(String[] args) {
       
       // 20 random numbers will be used for the test
       final int sz=20;
       Random rand = new Random();
       
       ArrayList<someObj> oList=new ArrayList<someObj>();
      
      // Build the list
      for(int i=0;i<sz;i++) {
          oList.add(new someObj(i,rand.nextFloat()));
      }
    
      // Print the list before sorting
      for(int i=0;i<sz;i++) { …
Run Code Online (Sandbox Code Playgroud)

java sorting arraylist comparator

2
推荐指数
1
解决办法
76
查看次数

标签 统计

arraylist ×1

comparator ×1

java ×1

sorting ×1