简体   繁体   English

将数组从一个 class 发送到另一个并排序

[英]Sending an array from one class to another and sorting

I need some help.我需要一些帮助。 So, recently I started working on a bit more complex stuff in Java and I got a task to make a program which will sort people and the amount of money they have in a descending order from the biggest amount of money to the smallest, but the array with the people's names is in one class, the amount of money is in another class and I have to send them to a third class and then sort them.所以,最近我开始在 Java 中研究一些更复杂的东西,我有一个任务来编写一个程序,该程序将人们和他们拥有的钱按从最大到最小的降序排列,但是带有人名的数组在一个 class 中,金额在另一个 class 中,我必须将它们发送到第三个 class 然后对其进行排序。 I tried to make it work and even asked my teacher but even the code he wrote for this doesn't work.我试图让它工作,甚至问我的老师,但即使他为此编写的代码也不起作用。 This is what I have so far.这就是我到目前为止所拥有的。

package classes;

public class Class {

    public static void main(String[] args) {

        String[]Name = {"Frodo","Princess Leia","Sansa Stark","Harry Potter","Spiderman"};
            Class3 send = new Class3();
            send.doesntmatter(Name);

    }
}

Second class with money.第二个 class 有钱。

package classes;

class Class2 {

    public static void main (String[]args) {

        int[]Money = {3000,400000,50000,100000,25};
            Class3 send = new Class3();
            send.doesntmatter(Money);
    }
}

And the third class where it all gets sorted.第三个 class 全部排序。

package classes;

class Class3 {

    public void doesntmatter(String[]Name,int[]Money) {

        for(int first = 0; first < 4; first++){

            int most = first;

            for(int current = first + 1; current < 4; current++){
                if (Money[current] > Money[most]){
                    most = current;
                }

                int temp = Money[most];
                Money[most] = Money[first];
                Money[first] = temp;

                String tempString = Name[most];
                Name[most] = Name[first];
                Name[first] = tempString;
            }

            for (int i = 0; i < 4; i++){
                System.out.println(Name[i]);
                System.out.println(Money[i]);
        }
    }
    }
}   

I know how to sort them but I have a problem with sending the arrays to an another class.我知道如何对它们进行排序,但是将 arrays 发送到另一个 class 时遇到问题。 Can somebody please help?有人可以帮忙吗? I hope you understand what's my problem.我希望你明白我的问题是什么。

You need to make doesntmatter function to RETURN the array you want:你需要让无关紧要的 function 返回你想要的数组:

public int[] doesntmatter(String[]Name,int[]Money) {

    for(int first = 0; first < 4; first++){

        int most = first;

        for(int current = first + 1; current < 4; current++){

            if (Money[current] > Money[most]){
                most = current;
            }

            int temp = Money[most];
            Money[most] = Money[first];
            Money[first] = temp;

            String tempString = Name[most];
            Name[most] = Name[first];
            Name[first] = tempString;
        }

        for (int i = 0; i < 4; i++){
            System.out.println(Name[i]);
            System.out.println(Money[i]);
        }
    }

    return Money;
}

This means that doesntmatter function will make a local copy of Money array, sort it, and return it.这意味着无关紧要的 function 将制作 Money 数组的本地副本,对其进行排序并返回。 Then in Class you need to change this code:然后在 Class 您需要更改此代码:

Money = send.doesntMatter(Money);

This last line will call doesntMatter with the unsorted array.最后一行将使用未排序的数组调用dontMatter。 The function will create a local copy, sort that local copy, return in, and then that local copy the function returns will be assigned to Money variable. function 将创建一个本地副本,对该本地副本进行排序,返回,然后将 function 返回的该本地副本分配给 Money 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM