Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.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/3/arrays/12.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 按列表中的指定值对ArrayList排序_Java_Arrays_Sorting_Arraylist - Fatal编程技术网

Java 按列表中的指定值对ArrayList排序

Java 按列表中的指定值对ArrayList排序,java,arrays,sorting,arraylist,Java,Arrays,Sorting,Arraylist,例如,我有一个数组: String[] Array1 = {"15", "1", "D1", "Wine", "1", "0", "15", "3", "D3", "Tap water", "2", "2", "15", "1", "M1", "Fish", "3", "0", "9", "5", "D4", "Coffee", "2", "2", "9", "2", "

例如,我有一个数组:

String[] Array1 = 
        {"15", "1", "D1", "Wine",       "1", "0", 
         "15", "3", "D3", "Tap water",  "2", "2",
         "15", "1", "M1", "Fish",       "3", "0",
         "9", "5", "D4", "Coffee",     "2", "2",
         "9", "2", "P2", "Cake",       "2", "1" 
        };
someList.addAll(Arrays.asList(Array1));

我想根据每行中的第二个值,即1,3,1,5,2,将类似于此的某种类型的ArrayList按数字顺序排序为1,1,2,3,5,同时保持同一行中的其他变量不变。我不允许创建另一个类来按顺序存储这些变量。有人知道我如何对它们进行那样的排序吗?

您使用了错误的数据结构。数组用于包含相同类型信息的相同类型的多个变量

我建议您创建一个类,并创建一个包含该类对象的数组,如下所示:

喝酒。上课

class Drink{
    private int a;
    private int b;
    private String c;
    private String drinkName;
    private int d;
    private int e;

    public Drink(int a,int b,String c,String drinkName,int d,int e){
        this.a=a;
        this.b=b;
        this.c=c;
        this.drinkName=drinkName;
        this.d=d;
        this.e=e;
    }

        // Getters and setters here
        public String getDrinkName(){
            return drinkName;
        }
        // .....
}
然后在你的主课上:

class MainClass{
    List<Drink> drinks;
    public static void main(String[] args){
        drinks = new ArrayList<>();
        drinks.add(new Drink(15,1,"D1","Wine",1,0));
        drinks.add(new Drink(15,3,"D3","Tap Water",2,2));
        // Etc...
        // You can retrieve elements using .get(index) and then use getters on it to retrieve informations
        System.out.println(drinks.get(0).getDrinkName());
        Collections.sort(drinks,new Comparator<Drink>(){
            @Override
            public int compare(Drink d1, Drink d2){
                // Edit this method as you need
                return d1.getA().compareTo(d2.getA());
            }

        });
    }
}
class类main类{
列出饮料清单;
公共静态void main(字符串[]args){
饮料=新的ArrayList();
饮料。添加(新饮料(15,1,“D1”,“葡萄酒”,1,0));
饮料。添加(新饮料(15,3,“D3”,“自来水”,2,2));
//等等。。。
//您可以使用.get(index)检索元素,然后使用其上的getter检索信息
System.out.println(drinks.get(0.getDrinkName());
Collections.sort(饮料、新比较器(){
@凌驾
公共整数比较(饮料d1、饮料d2){
//根据需要编辑此方法
返回d1.getA().compareTo(d2.getA());
}
});
}
}

您使用了错误的数据结构。数组用于包含相同类型信息的相同类型的多个变量

我建议您创建一个类,并创建一个包含该类对象的数组,如下所示:

喝酒。上课

class Drink{
    private int a;
    private int b;
    private String c;
    private String drinkName;
    private int d;
    private int e;

    public Drink(int a,int b,String c,String drinkName,int d,int e){
        this.a=a;
        this.b=b;
        this.c=c;
        this.drinkName=drinkName;
        this.d=d;
        this.e=e;
    }

        // Getters and setters here
        public String getDrinkName(){
            return drinkName;
        }
        // .....
}
然后在你的主课上:

class MainClass{
    List<Drink> drinks;
    public static void main(String[] args){
        drinks = new ArrayList<>();
        drinks.add(new Drink(15,1,"D1","Wine",1,0));
        drinks.add(new Drink(15,3,"D3","Tap Water",2,2));
        // Etc...
        // You can retrieve elements using .get(index) and then use getters on it to retrieve informations
        System.out.println(drinks.get(0).getDrinkName());
        Collections.sort(drinks,new Comparator<Drink>(){
            @Override
            public int compare(Drink d1, Drink d2){
                // Edit this method as you need
                return d1.getA().compareTo(d2.getA());
            }

        });
    }
}
class类main类{
列出饮料清单;
公共静态void main(字符串[]args){
饮料=新的ArrayList();
饮料。添加(新饮料(15,1,“D1”,“葡萄酒”,1,0));
饮料。添加(新饮料(15,3,“D3”,“自来水”,2,2));
//等等。。。
//您可以使用.get(index)检索元素,然后使用其上的getter检索信息
System.out.println(drinks.get(0.getDrinkName());
Collections.sort(饮料、新比较器(){
@凌驾
公共整数比较(饮料d1、饮料d2){
//根据需要编辑此方法
返回d1.getA().compareTo(d2.getA());
}
});
}
}

如果将所有元素作为
字符串
值存储到
数组
中,则无法轻松对元素进行排序。相反,您可以使用OOP定义名为
MyCustomData
的自定义类型(类),然后将数据作为对象加载

因此,您需要遵循以下步骤:

(1) 定义自定义类
MyCustomData

(2) 为
MyCustomData
创建对象并将其加载到数组中

(3) 现在,使用比较器对数组进行排序

您可以参考以下带有注释的代码:

MyCustomData类(正确命名该类):

public class MyCustomData {
        private int value1;//holds your element to be sorted
        //other values //define other values to hold fish, etc..

        public int getValue1() {
            return value1;
        }

        public void setValue1(int value1) {
            this.value1 = value1;
        }
    }
public static void main(String[] args) {

        MyCustomData[] myCustomDataArray = new MyCustomData[5];
        MyCustomData myCustomData1 = new MyCustomData();
        myCustomData1.setValue1(1);
        myCustomDataArray[0] = myCustomData1;
        //Create and Load other objects myCustomDataArray[1] , [2], ....into array 

        Comparator<MyCustomData> comp = (MyCustomData data1, MyCustomData data2) 
                             -> data1.getValue1()-data2.getValue1();
        Arrays.stream(myCustomDataArray).sorted(comp);
}
对MyCustomData数组进行排序:

public class MyCustomData {
        private int value1;//holds your element to be sorted
        //other values //define other values to hold fish, etc..

        public int getValue1() {
            return value1;
        }

        public void setValue1(int value1) {
            this.value1 = value1;
        }
    }
public static void main(String[] args) {

        MyCustomData[] myCustomDataArray = new MyCustomData[5];
        MyCustomData myCustomData1 = new MyCustomData();
        myCustomData1.setValue1(1);
        myCustomDataArray[0] = myCustomData1;
        //Create and Load other objects myCustomDataArray[1] , [2], ....into array 

        Comparator<MyCustomData> comp = (MyCustomData data1, MyCustomData data2) 
                             -> data1.getValue1()-data2.getValue1();
        Arrays.stream(myCustomDataArray).sorted(comp);
}
publicstaticvoidmain(字符串[]args){
MyCustomData[]myCustomDataArray=新的MyCustomData[5];
MyCustomData myCustomData1=新的MyCustomData();
myCustomData1.setValue1(1);
myCustomDataArray[0]=myCustomData1;
//创建其他对象myCustomDataArray[1]、[2]、…并将其加载到数组中
比较器comp=(MyCustomData数据1,MyCustomData数据2)
->data1.getValue1()-data2.getValue1();
流(myCustomDataArray).sorted(comp);
}

如果将所有元素作为
字符串
值存储到
数组
中,则无法轻松对元素进行排序。相反,您可以使用OOP定义名为
MyCustomData
的自定义类型(类),然后将数据作为对象加载

因此,您需要遵循以下步骤:

(1) 定义自定义类
MyCustomData

(2) 为
MyCustomData
创建对象并将其加载到数组中

(3) 现在,使用比较器对数组进行排序

您可以参考以下带有注释的代码:

MyCustomData类(正确命名该类):

public class MyCustomData {
        private int value1;//holds your element to be sorted
        //other values //define other values to hold fish, etc..

        public int getValue1() {
            return value1;
        }

        public void setValue1(int value1) {
            this.value1 = value1;
        }
    }
public static void main(String[] args) {

        MyCustomData[] myCustomDataArray = new MyCustomData[5];
        MyCustomData myCustomData1 = new MyCustomData();
        myCustomData1.setValue1(1);
        myCustomDataArray[0] = myCustomData1;
        //Create and Load other objects myCustomDataArray[1] , [2], ....into array 

        Comparator<MyCustomData> comp = (MyCustomData data1, MyCustomData data2) 
                             -> data1.getValue1()-data2.getValue1();
        Arrays.stream(myCustomDataArray).sorted(comp);
}
对MyCustomData数组进行排序:

public class MyCustomData {
        private int value1;//holds your element to be sorted
        //other values //define other values to hold fish, etc..

        public int getValue1() {
            return value1;
        }

        public void setValue1(int value1) {
            this.value1 = value1;
        }
    }
public static void main(String[] args) {

        MyCustomData[] myCustomDataArray = new MyCustomData[5];
        MyCustomData myCustomData1 = new MyCustomData();
        myCustomData1.setValue1(1);
        myCustomDataArray[0] = myCustomData1;
        //Create and Load other objects myCustomDataArray[1] , [2], ....into array 

        Comparator<MyCustomData> comp = (MyCustomData data1, MyCustomData data2) 
                             -> data1.getValue1()-data2.getValue1();
        Arrays.stream(myCustomDataArray).sorted(comp);
}
publicstaticvoidmain(字符串[]args){
MyCustomData[]myCustomDataArray=新的MyCustomData[5];
MyCustomData myCustomData1=新的MyCustomData();
myCustomData1.setValue1(1);
myCustomDataArray[0]=myCustomData1;
//创建其他对象myCustomDataArray[1]、[2]、…并将其加载到数组中
比较器comp=(MyCustomData数据1,MyCustomData数据2)
->data1.getValue1()-data2.getValue1();
流(myCustomDataArray).sorted(comp);
}

如果您创建了一些有用的方法和比较器,您可以使用经典的排序方法,如冒泡排序:

public static void main(String[] args) {    
    String[] array1 = 
        {"15", "1", "D1", "Wine",       "1", "0", 
         "15", "3", "D3", "Tap water",  "2", "2",
         "15", "1", "M1", "Fish",       "3", "0",
         "9", "5", "D4", "Coffee",     "2", "2",
         "9", "2", "P2", "Cake",       "2", "1" 
        };
    Comparator<String[]> comparator = new Comparator<String[]>(){
        @Override
        public int compare(String[]a1, String[] a2) {
            return Integer.valueOf(a1[1]).compareTo(Integer.valueOf(a2[1]));
        }
    };
    int lineLength=6;
    bubbleSort(array1,lineLength,comparator);
    System.out.println(Arrays.toString(array1));
}
//classic bubble-sort algorithm
public static void bubbleSort(String[]array1,int lineLength,Comparator<String[]> comparator){
    int numRow=array1.length/lineLength;
    for(int i=0;i<numRow;i++){
        for(int j=i+1;j<numRow;j++){
            String[] extractArrayI = extractArray(array1, i, lineLength);
            String[] extractArrayJ = extractArray(array1, j, lineLength);
            if(comparator.compare(extractArrayI, extractArrayJ)>0){
                swichLines(array1,i,j,lineLength);
            }
        }
    }
}
//extract i-th row
public static String[] extractArray(String[]array,int i, int lineLength){
    String [] a= new String[lineLength];
    System.arraycopy(array, i*lineLength, a, 0, lineLength);
    return a;
}
//Switch line i,j
public static void swichLines(String[]array,int i, int j,int lineLength){
    String [] temp = new String[lineLength];
    System.arraycopy(array, i*lineLength, temp, 0, lineLength);
    System.arraycopy(array, j*lineLength, array, i*lineLength, lineLength);
    System.arraycopy(temp, 0, array, j*lineLength, lineLength);
} 

如果您创建了一些有用的方法和比较器,您可以使用经典的排序方法,如冒泡排序:

public static void main(String[] args) {    
    String[] array1 = 
        {"15", "1", "D1", "Wine",       "1", "0", 
         "15", "3", "D3", "Tap water",  "2", "2",
         "15", "1", "M1", "Fish",       "3", "0",
         "9", "5", "D4", "Coffee",     "2", "2",
         "9", "2", "P2", "Cake",       "2", "1" 
        };
    Comparator<String[]> comparator = new Comparator<String[]>(){
        @Override
        public int compare(String[]a1, String[] a2) {
            return Integer.valueOf(a1[1]).compareTo(Integer.valueOf(a2[1]));
        }
    };
    int lineLength=6;
    bubbleSort(array1,lineLength,comparator);
    System.out.println(Arrays.toString(array1));
}
//classic bubble-sort algorithm
public static void bubbleSort(String[]array1,int lineLength,Comparator<String[]> comparator){
    int numRow=array1.length/lineLength;
    for(int i=0;i<numRow;i++){
        for(int j=i+1;j<numRow;j++){
            String[] extractArrayI = extractArray(array1, i, lineLength);
            String[] extractArrayJ = extractArray(array1, j, lineLength);
            if(comparator.compare(extractArrayI, extractArrayJ)>0){
                swichLines(array1,i,j,lineLength);
            }
        }
    }
}
//extract i-th row
public static String[] extractArray(String[]array,int i, int lineLength){
    String [] a= new String[lineLength];
    System.arraycopy(array, i*lineLength, a, 0, lineLength);
    return a;
}
//Switch line i,j
public static void swichLines(String[]array,int i, int j,int lineLength){
    String [] temp = new String[lineLength];
    System.arraycopy(array, i*lineLength, temp, 0, lineLength);
    System.arraycopy(array, j*lineLength, array, i*lineLength, lineLength);
    System.arraycopy(temp, 0, array, j*lineLength, lineLength);
} 

你好到目前为止你试过什么?你能提出这个问题吗?看起来你使用了错误的数据结构你的“行”在你的数据结构中没有这样标记-首先将它转换成一个行数组(带有一些“行”类型,例如数组),这会让事情变得更简单。然后,您只需要实现一个比较器,就可以使用标准的排序函数。完成了,不使用其他类(我使用的是primitive array,而不是ArrayList)嗨!到目前为止你试过什么?你能提出这个问题吗?看起来你使用了错误的数据结构你的“行”在你的数据结构中没有这样标记-首先将它转换成一个行数组(带有一些“行”类型,例如数组),这会让事情变得更简单。然后,您只需要实现一个比较器,就可以使用标准的排序函数。完成它,而不使用其他类(我使用prim