如何从 int 类型的方法中 return 多个整数

How to return multiple integers from a method of type int

如标题所述,我正在尝试从 int (int mode(int [] a) 类型的方法中 return 多个整数。该方法旨在接收一个整数数组并确定模式是什么。另一个问题是 int [] a 索引中的每个值都在 1-3 之间。我无法创建 objects 其他方法来处理部分解决方案或导入任何内容来帮助我。

我 运行 遇到的问题是数组中是否有多个模式

即。 int [] array = {1,2,2,3,3,4,5,6}

如您所见,在这种情况下有两种模式,即 2 和 3,我无法弄清楚如何从一种方法 return 多个整数。

这是我的教授说明

"Write the method int mode(int []a). This function takes an array a indexed from 1 to a.length. Each cell of a contains either 1,2, or 3. Your function returns the value (1,2,3) that occurs most frequently in array a. You are expected to not use any imports(ie. Math, arraylists, etc) or change the return type. Assume that main only contains an array, and then a call to the mode method using the aforementioned array in main. Here is the template: "

int 模式(int [] a){

}

我的代码如下所示:

public static int mode(int [] a){

int value = 0;
int countOne= 0;
int countTwo = 0;
int countThr = 0;

for(int i =0;i<a.length;i++){
    int count = 0;
    for(int j=i+1;j<a.length;j++){
        if(a[i] == a[j]){
            if(a[i] == 1){
                countOne++;
            }
            if(a[i] == 2){
                countTwo++;
            }   
            if(a[i] == 3){
                countThr++;
            }
        }

    }
}
   if(countOne > countTwo && countOne > countThr)
       value = countOne;
   if(countTwo > countOne && countTwo > countThr){
       value = countTwo;
   }
   if(countThr > countOne && countThr > countTwo){
       value = countThr;
   }
   /* if(countThr == countTwo){
          return countThr, countTwo;
   if(countOne == countTwo){
          //return countOne, countTwo;
   if(countOne == countThree){
          return countOne, countThree;
      */       

   return value;

`

main{ int [] a = {1,2,2,3,3}; System.out.println(modeTwo(a));

Output: 3

虽然三个是部分正确的,因为在这种情况下有多种模式,我想要的输出是

Desired Output: 2 3

在模式方法中,我只是为 1-3 创建一个计数器,以查看每个在数组中出现的次数。然后我设置条件来检查哪个是最好的。我试图创建条件来检查它们是否也相等,但是它不起作用,因为我必须 return 两个整数。我完全迷路了。任何帮助将不胜感激。

int 模式(int [] a){

}

所以你在这里需要做的是做除法和mod操作来提取你的return中的单个数字。

例如:

“期望的输出: 2 3

您的方法将 return 23。要提取 "ones" 列中的任何内容,请执行 num % 10。要提取 2,请将数字除以 10:num / 10 = secondDigit。如果您有一个三位数,请将其除以 100。

这就是您提取数字的方式。我将由您来构建 3 位数 return 值。

编辑

鉴于您关于无法使用 ArrayLists 或导入其他库的更新,我定制了一个 class 来计算出现次数 Modes 并且我仅使用数组来计算每个的最大出现次数数字,但只有 return 个 int[] 中出现次数等于最大出现次数的数字。

这是我能想到的,它将计算数组中每个数字的所有出现次数和 return 每个出现次数最多的数字,这与模式相同。

   public static void main(String eth[]) {
        int[] numbers = new int[] {1, 2, 2, 3, 3, 4, 4, 5, 5, 10, 12, 33};
        int[] modes = mode(numbers);

        for (int i = 0; i < modes.length; i++) {
            if (modes[i] == -999) { // Stop printing once you reach the first sentinal value
                continue;
            }
            System.out.println(modes[i]);
        }
    }

    private static int[] mode(int[] numbers) {
        Modes[] modes = new Modes[numbers.length];
        int modesIndex = 0;

        int maxOccurrence = 0;
        for (int i = 0; i < numbers.length; i++) {
            if (numbers[i] == -999) {
                continue;
            }

            int number = numbers[i];
            numbers[i] = -999; // -999 is a sentinel value

            int count = 1;
            for (int j = i + 1; j < numbers.length; j++) {
                if (numbers[j] == -999) {
                    continue;
                }

                if (numbers[j] == number) {
                    numbers[j] = -999;
                    count++;
                }
            }
            if (count > maxOccurrence) {
                maxOccurrence = count;
            }
            modes[modesIndex++] = new Modes(number, count);
        }

        int[] result = new int[numbers.length];
        for (int i = 0; i < result.length; i++) {
            result[i] = -999; // Sentinel value
        }

        int index = 0;
        for (int i = 0; i < modes.length; i++) {
            if (modes[i] == null) {
                break; // Stop when you hit the first null
            }
            if (modes[i].Occurrences == maxOccurrence) {
                result[index] = modes[i].Number;
                index++;
            }
        }

        return result;
    }

    public static class Modes {
        public int Number;
        public int Occurrences;

        public Modes(int number, int occurrences) {
            Number = number;
            Occurrences = occurrences;
        }
    }

结果: