Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:从索引到索引对列表进行排序_Java_Arrays_List_Sorting - Fatal编程技术网

Java:从索引到索引对列表进行排序

Java:从索引到索引对列表进行排序,java,arrays,list,sorting,Java,Arrays,List,Sorting,对于数组,有一个特殊函数用于将数组的一部分从一个索引排序到另一个索引: 对于列表 还有一个排序功能 不幸的是,没有接受from index和to index参数的变量 我知道我可以通过以下两种方法解决这个问题 将列表转换为数组并应用数组。排序,然后将其转换回列表 将按fromIndex索引到toIndex索引的列表项复制到新列表(使用list.subList(fromIndex,toIndex)),对其排序并覆盖旧列表项 但是我希望有一种更好的方法来实现这一点。只需使用.subList(

对于数组,有一个特殊函数用于将数组的一部分从一个索引排序到另一个索引:

对于
列表

还有一个排序功能

不幸的是,没有接受
from index
to index
参数的变量

我知道我可以通过以下两种方法解决这个问题

  • 将列表转换为数组并应用
    数组。排序
    ,然后将其转换回列表
  • 将按fromIndex索引到toIndex索引的列表项复制到新列表(使用
    list.subList(fromIndex,toIndex)
    ),对其排序并覆盖旧列表项
但是我希望有一种更好的方法来实现这一点。

只需使用.subList()将“backed”视图放到主列表中,然后调用sort。子列表是“直写”的,因此更改会反映在原始列表中

List<Integer> foo = Arrays.asList(5,3,1,6,2,1);
Collections.sort(foo.subList(0, 3)); // sort first 3 elements 
System.out.println(foo);
Collections.sort(foo.subList(3, 6)); // sort last 3 elements
System.out.println(foo);

您可以在原始列表上使用
subList()
,然后对子列表进行排序,它将反映在原始列表上,而无需回写。

查看Oracle文档,
Collections
list
数组一样,不包含此功能。如果我必须在您的两个建议中进行选择,我会使用
List.subList(fromIndex,toIndex))
实现第二个建议

以下是文档:

将按fromIndex到toIndex索引的列表项复制到新列表 (通过使用list.subList(fromIndex,toIndex)),对其进行排序并覆盖 旧列表条目

否,调用list.subList时没有对象副本。函数子列表创建一个由原始列表支持的视图。仅参考副本;没有实际的对象副本

视图上的任何操作(排序)都将反映在原始列表上

 public static void main(String[] args) throws Exception {
    List<Integer> list = Arrays.asList(1, 9, 8 ,7, 2, 3, 4);

    // [9, 8 ,7] => [7, 8, 9]
    sortList(list, 1, 4);

    System.out.println(list);       // [1, 7, 8, 9, 2, 3, 4]
  }

  public static <T extends Comparable<T>> void sortList(
      List<T> list, int fromIndex, int toIndex) {
    Collections.sort(list.subList(fromIndex, toIndex));
  }
publicstaticvoidmain(字符串[]args)引发异常{
List=Arrays.asList(1,9,8,7,2,3,4);
// [9, 8 ,7] => [7, 8, 9]
分类表(列表,1,4);
System.out.println(列表);//[1,7,8,9,2,3,4]
}
公共静态无效排序列表(
列表列表,int-fromIndex,int-toIndex){
Collections.sort(list.subList(fromIndex,toIndex));
}

嘿,伙计,转换成数组,享受一些额外的功能,然后再转换回来,这并没有什么丢脸的;)
 public static void main(String[] args) throws Exception {
    List<Integer> list = Arrays.asList(1, 9, 8 ,7, 2, 3, 4);

    // [9, 8 ,7] => [7, 8, 9]
    sortList(list, 1, 4);

    System.out.println(list);       // [1, 7, 8, 9, 2, 3, 4]
  }

  public static <T extends Comparable<T>> void sortList(
      List<T> list, int fromIndex, int toIndex) {
    Collections.sort(list.subList(fromIndex, toIndex));
  }