简体   繁体   English

如何从int类型的方法返回多个整数

[英]How to return multiple integers from a method of type int

As the title states, I'm attempting to return multiple integers from a method of type int ( int mode(int [] a ) . The method is designed to take in an array of integers and determine what the mode(s) would be. Another catch is that each value in the index of int [] a will range from 1-3. I cannot create objects, other methods to handle part of the solution or import anything to help me. 正如标题所述,我试图从int类型的方法返回多个整数( int mode(int [] a )。该方法被设计为接受一个整数数组并确定模式是什么样的另一个问题是int [] a索引中的每个值都在1-3之间。我无法创建对象,其他方法可以处理部分解决方案或导入任何东西来帮助我。

The problem I'm running into is if there are multiple modes in the array 我遇到的问题是数组中是否有多种模式

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

As you can see, there's two modes in this case which are 2 and 3 and I'm not able to figure out how to return multiple integers from one method. 正如您所看到的,在这种情况下有两种模式是2和3,我无法弄清楚如何从一种方法返回多个整数。

HERE ARE MY PROFESSORS INSTRUCTIONS" 这是我的教授说明“

"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)。此函数采用从1到a.length索引的数组.a的每个单元格包含1,2或3.您的函数返回值(1,2, 3)最常出现在数组a中。你应该不使用任何导入(即数学,arraylists等)或改变返回类型。假设main只包含一个数组,然后使用mode方法调用主要的上述数组。这是模板:“

int mode(int [] a){ int mode(int [] a){

} }

Here's what my code looks like: 这是我的代码的样子:

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

While three is partially correct, since there a multiple modes in this case my desired output is 虽然三个部分是正确的,因为在这种情况下有多个模式我的期望输出是

Desired Output: 2 3

In the mode method I'm just creating a counter for 1-3 to see how many times each occurs in the array. 在模式方法中,我只是为1-3创建一个计数器,以查看每个数组中出现的次数。 Then I set up conditionals to check which is greatest. 然后我设置条件来检查哪个是最大的。 I attempted to create conditions to check if they were equal as well, however it wouldn't work since I'd have to return two integers. 我试图创建条件来检查它们是否相等,但是由于我必须返回两个整数,所以它不起作用。 I'm just completely lost. 我完全迷失了。 Any help would be appreciated. 任何帮助,将不胜感激。

int mode(int [] a){ int mode(int [] a){

} }

So what you need to do here is to do divide and mod operations to extract individual digits in your return. 所以你需要做的是做除法和模式运算来提取你的回报中的个别数字。

For example: 例如:

"Desired Output: 2 3" “期望输出:2 3”

Your method will return 23. To extract anything in the "ones" column, do num % 10. To extract the 2, divide the number by 10: num / 10 = secondDigit. 你的方法将返回23.要在“ones”列中提取任何内容,请执行num%10。要提取2,请将数字除以10:num / 10 = secondDigit。 If you have a three digit number, divide it by 100. 如果您有三位数字,请将其除以100。

This is how you extract the numbers. 这是你提取数字的方法。 I will leave it up to you to construct the 3 digit return value. 我将把它留给你构建3位数的返回值。

EDIT 编辑

Given your update about not being able to use ArrayLists or import other libraries, I made a custom class for counting occurrences Modes and am using just arrays to still count the max occurrences of each number, but only return the numbers in an int[] whose occurrences equal the max occurrences. 鉴于您无法使用ArrayLists或导入其他库的更新,我创建了一个自定义类来计算出现的Modes ,我只使用数组来计算每个数字的最大出现次数,但只返回int[]的数字出现次数等于最大出现次数。

Here's what I could come up with that will count all occurrences of each number in the array and return each number that has the max occurrences, which is the same as the mode. 这是我能想到的,它将计算数组中每个数字的所有出现次数,并返回具有最大出现次数的每个数字,这与模式相同。

   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;
        }
    }

Result: 结果:

在此输入图像描述

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

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