Java 使用Arrays.Sort()对并行数组进行排序
是否可以使用Java 使用Arrays.Sort()对并行数组进行排序,java,arrays,sorting,parallel-arrays,Java,Arrays,Sorting,Parallel Arrays,是否可以使用Arrays.sort()对数组进行排序,然后将另一个相关数组定位为与排序后的数组相同的位置,例如: String arrNames[] = new String[5]; String arrCellNo[] = new String[arrNames.length]; String arrNamesSorted[] = new String[arrNames.length]; System.arraycopy(arrNames, 0, arrNa
Arrays.sort()
对数组进行排序,然后将另一个相关数组定位为与排序后的数组相同的位置,例如:
String arrNames[] = new String[5];
String arrCellNo[] = new String[arrNames.length];
String arrNamesSorted[] = new String[arrNames.length];
System.arraycopy(arrNames, 0, arrNamesSorted, 0, arrNames.length);
Arrays.sort(arrNamesSorted);
从这一点开始,我想对
CellNo
数组进行排序,这样,如果“person”有一个CellNo“x”,那么在数组arrNames
排序后,他将有相同的“CellNo”“x”你不能有数组。排序操作第二个数组,就像对第一个数组排序一样
解决方案是对包含所有所需数据的对象进行排序。创建一个带有名称和单元格编号属性的Contact
类。然后创建一个实现(比如,ContactComparator
)的类来比较名称
然后,您将能够使用对Contact
对象数组进行排序
所有数据都将保持组织状态,因为相同的名称仍将具有相同的单元格编号。您不能拥有数组。排序操作第二个数组的方式与对第一个数组排序的方式相同
解决方案是对包含所有所需数据的对象进行排序。创建一个带有名称和单元格编号属性的Contact
类。然后创建一个实现(比如,ContactComparator
)的类来比较名称
然后,您将能够使用对Contact
对象数组进行排序
所有数据都将保持有序,因为相同的名称仍将具有相同的单元格编号。我将采用不同的方法:
创建新对象:
public class Person {
private name;
private cellNo;
// Implement getters and setters
}
创建一个比较器:
public MyComparator implements Comparator<Person> {
public int compare(Person a, Person b) {
return a.getName().compareTo(b.getName());
}
}
公共MyComparator实现比较器{
公共int比较(人员a、人员b){
返回a.getName().compareTo(b.getName());
}
}
在Person[]persons=…
数组上调用Array.sort(persons,new MyComparator())
我会选择一种不同的方法:
创建新对象:
public class Person {
private name;
private cellNo;
// Implement getters and setters
}
创建一个比较器:
public MyComparator implements Comparator<Person> {
public int compare(Person a, Person b) {
return a.getName().compareTo(b.getName());
}
}
公共MyComparator实现比较器{
公共int比较(人员a、人员b){
返回a.getName().compareTo(b.getName());
}
}
在Person[]persons=…
数组上调用Array.sort(persons,new MyComparator())
如果名称是唯一的,请考虑使用A:
final SortedMap nameToCellNo=new TreeMap();
for(int i=0;i <代码> > p>如果名称是唯一的,请考虑使用:
final SortedMap nameToCellNo=new TreeMap();
for(int i=0;i
我发现答案中引入的一些概念很难理解,因此在我自己的解决方案中,我采用了不受欢迎的编程方法,以换取更容易理解的代码,并创建了冒泡排序方法,最后像这样操纵第二个数组:
String arrNames[] = new String[5];
String arrCellNo[] = new String[arrNames.length];
String arrNamesSorted[] = new String[arrNames.length];
String arrCellNoSorted[] = new String[arrCellNo.length];
System.arraycopy(arrNames, 0, arrNamesSorted, 0, arrNames.length);
System.arraycopy(arrCellNo, 0, arrCellNoSorted, 0, arrCellNo.length);
for (int i = 0; i < arrNamesSorted.length; i++) {
for (int j = 0; j <arrNamesSorted.length-1; j++) {
if (arrNamesSorted[j].compareTo( arrNames[j+1])>0) {
String temp = arrNamesSorted[i];
arrNamesSorted[i] = arrNamesSorted[j];
arrCellNoSorted[i] = arrCellNoSorted[j];
arrNames[j] = temp;
}
}
}
String arrNames[]=新字符串[5];
字符串arrCellNo[]=新字符串[arrNames.length];
字符串arrNamesSorted[]=新字符串[arrNames.length];
字符串arrCellNoSorted[]=新字符串[arrCellNo.length];
System.arraycopy(arrNames,0,arrNamesSorted,0,arrNames.length);
系统数组复制(arrCellNo,0,arrCellNoSorted,0,arrCellNo.length);
for(int i=0;i
我发现答案中引入的一些概念很难理解,因此在我自己的解决方案中,我采用了不受欢迎的编程方法,以换取更容易理解的代码,并创建了冒泡排序方法,最后像这样操纵第二个数组:
String arrNames[] = new String[5];
String arrCellNo[] = new String[arrNames.length];
String arrNamesSorted[] = new String[arrNames.length];
String arrCellNoSorted[] = new String[arrCellNo.length];
System.arraycopy(arrNames, 0, arrNamesSorted, 0, arrNames.length);
System.arraycopy(arrCellNo, 0, arrCellNoSorted, 0, arrCellNo.length);
for (int i = 0; i < arrNamesSorted.length; i++) {
for (int j = 0; j <arrNamesSorted.length-1; j++) {
if (arrNamesSorted[j].compareTo( arrNames[j+1])>0) {
String temp = arrNamesSorted[i];
arrNamesSorted[i] = arrNamesSorted[j];
arrCellNoSorted[i] = arrCellNoSorted[j];
arrNames[j] = temp;
}
}
}
String arrNames[]=新字符串[5];
字符串arrCellNo[]=新字符串[arrNames.length];
字符串arrNamesSorted[]=新字符串[arrNames.length];
字符串arrCellNoSorted[]=新字符串[arrCellNo.length];
System.arraycopy(arrNames,0,arrNamesSorted,0,arrNames.length);
系统数组复制(arrCellNo,0,arrCellNoSorted,0,arrCellNo.length);
for(int i=0;i
可以使用内置的array.sort存档效果,而无需为并行数组内容创建类
请注意,索引应该是对象数组,而不是基元数组。(Arrays.sort(int[])
不使用比较器)
评论
foreach::Num->(Num->void)
(并行)par_foreach::Num->(Num->void)
如果您无法想象实现:
可以使用内置的array.sort存档效果,而无需为并行数组内容创建类
请注意,索引应该是对象数组,而不是基元数组。(Arrays.sort(int[])
不使用比较器)
评论
foreach::Num->(Num->void)
(并行)par_foreach::Num->(Num->void)
如果您无法想象实现: