我正在尝试创建10个无序整数的链接列表,输出列表,然后在列表中找到最小的元素并将其输出。这就是我所拥有的。它查找并输出一个元素,但不输出列表中的最小元素。请帮忙。

import java.util.LinkedList;
import java.util.Random;

public class intergers
{

    public static void main(String[] args)
    {
        LinkedList <Integer> integers = new LinkedList<Integer>();

        //Generates an unordered list of ten integers.
        Random Doge = new Random();


        //Using a for loop.

        for(int count =0; count<10; count++)
        {
            int integer = Doge.nextInt(10);
            integers.add(integer);
        }
        if(integers.getLast() !=0)
        {
            integers.removeLast();
            integers.add(0);
        }

        System.out.println(integers); //Prints out that list.
        int Oedipus;
        for(Oedipus =0; Oedipus <integers.size()-1; Oedipus++)
        {
            if(integers.get(Oedipus) < integers.get(Oedipus++))
            {
                //int smallest = integers.get(Oedipus);
                int smallest = integers.get(Oedipus);
            }

            if(integers.get(Oedipus) > integers.get(Oedipus++))
            {
                //int smallest = integers.get(Oedipus);
                //System.out.println("Smallest " + integers.get(Oedipus));
            }
            int smallest = integers.size();
        }
        int smallest = integers.get(Oedipus);
        System.out.println("The smallest element is: " + smallest);

    }
}

最佳答案

您是否尝试过使用Collections.min()方法?

10-01 21:19