java - 在 ArrayList 中查找最大数

标签 java arrays arraylist

这是我即将进行的测试之前的练习,我试图让用户输入一个数字。 array1 中低于用户编号的所有元素将放入新的 ArrayList 中。 然后我试图只打印该 ArrayList 中的最高数字。如果用户输入小于 array1 中的所有数字,它将返回 -1。 这是我的代码,但是,当我输入 920 时,它仍然返回 -1,我认为我的代码在 ArrayList 中查找最高数字时有问题。你们能告诉我哪里出了问题吗?

static Scanner sc = new Scanner(System.in);
static int[] array1 = {900, 885, 989, 1000, 1500, 1243, 999, 915};

public static int blabla(int[] a, int b) {

Integer d = -1;
ArrayList<Integer> al = new ArrayList<Integer>();

    for (int i = 0; i < array1.length; i++) { // this is to find all numbers in array1 that is below user's number, and add it to the ArrayList
        if (b > array1[i]) {
        al.add(array1[i]);
        } // if
    } // for

    outerloop: // and this function below is to find maximum number in ArrayList
    for (int g = (al.size()-1); g == 0; g--) {
                for (int j = 0; j <=(g-1); j++) {
                    if (al.get(j) > al.get(g)) {
                        break;
                    }
                    else if(j == (g-1)) {
                        if (al.get(g) > al.get(j)){
                            d = al.get(g);
                            break outerloop;
                        }
                    }
                } //for^2
    } // for
return d;
} // priceisright

最佳答案

static Scanner sc = new Scanner(System.in);
static int[] array1 = {900, 885, 989, 1000, 1500, 1243, 999, 915};

public static int blabla(int[] a, int b) {

Integer d = -1;
ArrayList<Integer> al = new ArrayList<Integer>();

此时a1是一个空数组,所以a1.length = 0,这个循环永远不会执行。

    for (int i = 0; i < a1.length; i++) { 
        // this is to find all numbers in array1 that is below user's number, 
        // and add it to the ArrayList
        if (b > a1[i]) {
            al.add(a1[i]);
        } // if
    } // for

a1 那里仍然是空的,第二个循环也不会做任何事情。

    // and this function below is to find maximum number in ArrayList
    outerloop: 
    for (int g = (al.size()-1); g == 0; g--) {
                for (int j = 0; j <=(g-1); j++) {
                    if (al.get(j) > al.get(g)) {
                        break;
                    }
                    else if(j == (g-1)) {
                        if (al.get(g) > al.get(j)){
                            d = al.get(g);
                            break outerloop;
                        }
                    }
                } //for^2
    } // for
return d;
} // priceisright

这个怎么样:

    // Finds the greater value in values that is below maximum.
    // Returns -1 if none is found. 
    public static int blabla(int[] values, int maximum) {
      int best_value = -1;
      for (int value : values) {
        if (value < maximum && value > best_value) {
          best_value = value;
        }
      }

      return best_value;
    }

你可以替换int[] values通过 List<Integer> values如果您的值在 ArrayList 中。

关于java - 在 ArrayList 中查找最大数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24352597/

相关文章:

java - 获取字符串的哈希值作为字符串

c++ - 将二维数组元素复制到另一个具有另一个大小的二维数组

c# - 嵌套的 foreach 循环将 DirectoryInfo.GetDirectories 和 GetFiles 添加到数组?

java - 迭代 ArrayList 添加值

java - 建议从 struts1 → (struts2, spring) 的进阶路径

java - 使用外部类锁获取内部类锁?

java - 通过 Socket 传递 TreeMap 对象(从服务器到客户端)

c - C语言程序从文件中读取信息

Java ArrayList 和对象问题。数值重置

java - 我的 Java 类 addUserToTheList() 不起作用