java—如何在数组/数组列表中存储和读取扫描仪输入?

ohtdti5x  于 2021-07-06  发布在  Java
关注(0)|答案(4)|浏览(362)

我想写一个程序,打印出存储在计算机中的第三大数字 ArrayList . 我尝试对其排序,然后将列表反转为,并使用get()方法获取第三高数字的索引。但它并没有考虑到如果在前3个插槽中有重复的情况。
如何遍历arraylist以获得第三高值?
到目前为止我所做的一切。

import java.util.*;

public class third {
    public static void main(String[] args) {

        ArrayList<Integer> numbers = new ArrayList<>(); //creating an array object
        Scanner scan = new Scanner(System.in); // scanner to read user input

        // print statement to get input
        System.out.print("Enter any amount of numbers: "); 
        int i = 0;
        while (scan.hasNextInt()) {// while there is an hasnext has an int                
            numbers.add(scan.nextInt()); //adding input(numbers) to an arraylist
            i++; //increment by 1                     
        } 
        scan.close(); // close the scanner if the user doesnt enter a number.

        System.out.println("you entered: " + numbers);
        System.out.println("The largest number you entered is: " + Collections.max(numbers));//print largest number
        System.out.println("The smallest number you entered is: " + Collections.min(numbers));//print smallest number

        Collections.sort(numbers); //sorting the numbers from lowest to highest
        System.out.println("Sorted: " + numbers); //print sorted numbers

        Collections.reverse(numbers); //reverse the order to use to get the third highest value
        System.out.println("Highest to lowest: " + numbers);
        System.out.println("Third highest number is:" + numbers.get(2));//get the third index of the sorted ArrayList       
        }
}
gzjq41n4

gzjq41n41#

将arraylist值传递给hashmap。或者将值存储在hashmap中。hashmap不包含重复项。在那之后,倒转收集和做你做的。

t30tvxxf

t30tvxxf2#

最后需要一个循环来丢弃重复项并找到精确的第三大数。就像我所做的。

import java.util.*;

public class third {
public static void main(String[] args) {

    ArrayList<Integer> numbers = new ArrayList<>(); //creating an array object
    Scanner scan = new Scanner(System.in); // scanner to read user input

    // print statement to get input
    System.out.print("Enter any amount of numbers: "); 
    int i = 0;
    while (scan.hasNextInt()) {// while there is an hasnext has an int                
        numbers.add(scan.nextInt()); //adding input(numbers) to an arraylist
        i++; //increment by 1                     
    } 
    scan.close(); // close the scanner if the user doesnt enter a number.

    System.out.println("you entered: " + numbers);
    System.out.println("The largest number you entered is: " + Collections.max(numbers));//print largest number
    System.out.println("The smallest number you entered is: " + Collections.min(numbers));//print smallest number

    Collections.sort(numbers); //sorting the numbers from lowest to highest
    System.out.println("Sorted: " + numbers); //print sorted numbers

    Collections.reverse(numbers); //reverse the order to use to get the third 
    highest value
    System.out.println("Highest to lowest: " + numbers);
    int counter=0; // To find distinct number 
    for(int i=numbers.size()-1 ; i>=0 ; i--){ // Another loop to by pass 
    //duplicates
    if(numbers.get(i)!=numbers.get(i-1)){
    counter++;
    }
    if(counter==2){      // third highest
      System.out.println("Third highest number is:" + numbers.get(i));//get
    //third index of the sorted ArrayList 
    break;
    }

    }
    }
csbfibhn

csbfibhn3#

使用sortedset对重复项进行排序和删除,因此 Collections.sort(numbers); 您可以使用(草稿,不检查):

SortedSet<Integer> numbersSet = new TreeSet<>(numbers );
 numbers = new ArrayList<>(numbersSet);
 Collections.reverse(numbers);        

 Collections.reverse(numbers); //reverse the order to use to get the third highest value
 System.out.println("Highest to lowest: " + numbers);
 System.out.println("Third highest number is:" + numbers.length() > 2? numbers.get(2): "not found");//get the third index of the sorted ArrayList
yftpprvb

yftpprvb4#

为了找到第三大元素,只需从数字列表中创建一个sortedset。如果集合至少包含三个元素,那么最后三个元素就是您想要的元素。为了得到最后的第三个元素,将 SortedSet 到一个数组。参见下面的代码。请注意 SortedSet 是由类treeset实现的接口。

SortedSet<Integer> ss = new TreeSet<>(numbers);
if (ss.size() > 2) {
    System.out.println("Third highest number is:" + ss.toArray(new Integer[0])[ss.size() - 3]);//get the third index of the sorted ArrayList
}

相关问题