从最高顺序到最低顺序,并从另一个arraylist java更改位置

从最高顺序到最低顺序,并从另一个arraylist java更改位置,java,android,sorting,arraylist,compare-and-swap,Java,Android,Sorting,Arraylist,Compare And Swap,我有两个arraylist int intArray[] = new int[]{4, 2, 2, 6, 2}; String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing} int n = intArray.le

我有两个arraylist

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
一个是整数数组,另一个是字符串数组。这里我需要对整数数组值进行排序,从最高值排序到最低值。此时,我还需要移动字符串数组中的位置

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
例如:

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
字符串数组:[注册和行政主管,网页设计,IT-软件工程师,会计,网络工程师] 整数数组:[4,2,2,6,2]

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
但我需要上面的结果

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
字符串数组:[会计、注册和行政主管、IT-软件工程师、网络工程师、网站设计] 整数数组:[6,4,2,2,2]

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
需要对整数数组进行排序,同时更改字符串数组位置,如果整数数组中出现相同的值,则需要按字母顺序排序。我该怎么办

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }

有没有最短的方法?

在您的情况下,数组的小尺寸意味着使用冒泡排序(冒泡排序对于大数组来说不是一种高性能)。根据位置对int数组进行排序您交换的字符串数组也是这样

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
intintarray[]=newint[]{4,2,2,6,2};
String strArray[]=新字符串[]{会计、注册和行政主管、IT-软件工程师、网络工程师、网站设计}
int n=intArray.length;
int temp=0,temp1=0;
对于(int i=0;i
List data=new ArrayList试试这样的方法

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
这将是您的数据结构

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
class DataModel{
  int priority;
  String priorityName;
  DataModel(int priority,String priorityName){
       this.priority = priority; 
       this.priorityName = priorityName;
   }
}
现在为这个类创建一个Arraylist
dataArrayList

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
使用方法按优先级对int数组进行排序

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
for(int i=0; i < n; i++){
   for(int j=1; j < (n-i); j++){

            if(intArray[j-1] < intArray[j]){

             //add the data of your largest int number in your int Array and use its position to get the data of your String array. 
             dataArrayList.add(new DataModel(your_lagest_int, your_department)); 

        }
    }
}
for(int i=0;i

您应该得到一个包含所有数据的排序Arraylist

为什么不使用第三类,将所有字符串按权重分组

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
创建一个比较器来排序WeightString,然后使用流API来使用数据

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
public class WeightString {
    public String value;
    public int weight;

    public WeightString(String value, int weight) {
        this.value = value;
        this.weight = weight;
    }
}

public Comparator<WeightString> mWeightStringComparator = 
        (o1, o2) -> Integer.compare(o1.weight, o2.weight);


// in code
WeightString[] weightStrings = new WeightString[]{
        new WeightString("a", 12),
        new WeightString("b", 1),
        new WeightString("c", 8),
        new WeightString("d", 4)
};

Stream<WeightString> stringStream = Arrays.stream(weightStrings);
stringStream.sorted(mWeightStringComparator);
WeightString[] orderedWeightStrings = (WeightString[]) stringStream.toArray();

//orderedWeightStrings : [b,d,c,a]
公共类权重字符串{
公共字符串值;
公共权重;
公共权重字符串(字符串值,整数权重){
这个值=值;
重量=重量;
}
}
公共比较器mWeightStringComparator=
(o1,o2)->整数。比较(o1.重量,o2.重量);
//编码
WeightString[]weightStrings=新的WeightString[]{
新的权重字符串(“a”,12),
新的权重字符串(“b”,1),
新的权重字符串(“c”,8),
新权重字符串(“d”,4)
};
Stream stringStream=Arrays.Stream(weightStrings);
stringStream.sorted(mWeightStringComparator);
WeightString[]OrderedWeightString=(WeightString[])stringStream.toArray();
//OrderedWeightString:[b、d、c、a]

我希望这能帮助您:

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
int[] intArray = new int[] { 4, 2, 2, 6, 2 };
String[] strArray = new String[] { "Register & Head of Administration", "Web Designing", "IT - Software Engineer", "Accountant", "Network Engineer" };
int tmp0 = 0;
String tmp1 = "";
for (int i = 0; i < intArray.length; i++) {

    for (int j = i + 1; j < intArray.length; j++) {

        if (intArray[j] > intArray[i]) {

            // swap in int-Array
            tmp0 = intArray[i];
            intArray[i] = intArray[j];
            intArray[j] = tmp0;

            // swap in string-Array
            tmp1 = strArray[i];
            strArray[i] = strArray[j];
            strArray[j] = tmp1;
        } else if (intArray[j] == intArray[i]) {

            // sorts alphabetically
            if (strArray[j].compareTo(strArray[i]) < 0) {

                tmp1 = strArray[i];
                strArray[i] = strArray[j];
                strArray[j] = tmp1;
            }
        }
    }
}

//output
for (int k = 0; k < intArray.length; k++) {

    System.out.println(strArray[k] + " " + intArray[k]);
}
int[]intArray=newint[]{4,2,2,6,2};
String[]strArray=新字符串[]{“注册和行政主管”、“网站设计”、“IT-软件工程师”、“会计”、“网络工程师”};
int-tmp0=0;
字符串tmp1=“”;
for(int i=0;iintArray[i]){
//整数数组中的交换
tmp0=intArray[i];
intArray[i]=intArray[j];
intArray[j]=tmp0;
//字符串数组中的交换
tmp1=strArray[i];
strArray[i]=strArray[j];
strArray[j]=tmp1;
}else if(intArray[j]==intArray[i]){
//按字母顺序排序
if(strArray[j].compareTo(strArray[i])<0{
tmp1=strArray[i];
strArray[i]=strArray[j];
strArray[j]=tmp1;
}
}
}
}
//输出
对于(int k=0;k
输出:

            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }
会计6 登记册及行政总监4 资讯科技-软件工程师2 网络工程师2
Web设计2

最合理的方法是将2个ArrayList更改为1个ArrayList对象,其中类将有2个字段:字符串名称和int值。在这个类中,还实现了基于值的intefrace Compariable。然后它将只使用Collections.sort()它将工作,但是:1。你不知道数组中的项数,所以冒泡排序可能不是最好的,第二-为什么要写这么多代码,当事情可以在你的情况下做得很好时数组的小尺寸意味着使用冒泡排序(冒泡排序对大数组来说不是一种高性能)不确定,如果你发现了,但是有重复的值(因此,在您的情况下,重复的密钥将不起作用)
            int intArray[] = new int[]{4, 2, 2, 6, 2};
            String strArray[]=new String[]{Accountant, Register & Head of Administration, IT - Software Engineer, Network Engineer,Web Designing}
            int n = intArray.length;
            int temp = 0,temp1=0;               
            for(int i=0; i < n; i++){
                    for(int j=1; j < (n-i); j++){                               
                            if(intArray[j-1] < intArray[j]){                                        
                                    temp = intArray[j-1];
                                    temp1=strArray[j-1];
                                    intArray[j-1] = intArray[j];
                                    strArray[j-1] = strArray[j];
                                    intArray[j] = temp;
                                    strArray[j] = temp1;
                            }                               
                    }
            }