[英]Smallest element in a Linked List in Java
I am trying to create a linked list of 10 unordered integers, output the list, then find the smallest element in the list and output it. 我正在尝试创建10个无序整数的链接列表,输出列表,然后在列表中找到最小的元素并将其输出。 This is what I have.
这就是我所拥有的。 It finds and output an element but not the smallest in the list.
它查找并输出一个元素,但不输出列表中的最小元素。 Please help.
请帮忙。
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()方法?
This line 这条线
System.out.println(integers); //Prints out that list.
does not do what the comment says it does. 不按照评论说的去做。 Add a loop to go through the items, and print each item individually to see what's in the list.
添加一个循环来遍历所有项目,并分别打印每个项目以查看列表中的内容。
Your lines that deal with smallest
, eg this one 您处理
smallest
,例如此行
int smallest = integers.get(Oedipus);
are incorrect for two reasons: they declare a new smallest
every time in a nested scope, and they do not initialize smallest
to begin with. 之所以不正确,有两个原因:它们每次在嵌套作用域中都声明一个新的
smallest
,并且它们不初始化smallest
就开始。
To fix this, declare smallest
outside the loop, initialize it with the initial element of the list, and then iterate elements starting from 1
, comparing it to smallest
. 要解决此问题,请在循环外部声明
smallest
,然后使用列表的初始元素对其进行初始化,然后迭代从1
开始的元素,并将其与smallest
比较。 Alternatively, store the index , not the value, of the smallest element in the smallest
. 或者,将最小元素的索引而不是值存储在
smallest
。 Initialize it with zero, and set it to Oedipus
if you find an element that is smaller than the one at integers.get(smallest)
. 将其初始化为零,如果找到一个小于
integers.get(smallest)
的元素的元素,则将其设置为Oedipus
。
You just need to iterate through the list and try to find the minimum element. 您只需要遍历列表并尝试查找最小元素。
int minInt = Integer.MAX_VALUE
for(int i = 0; i < Integers.size(), ++i){
if(Integers.get(i) < minInt)
minInt = Integers.get(i)
}
Edit: The reason I stored the value rather than the index is because random access of linked lists is slow. 编辑:之所以我存储值而不是索引是因为对链表的随机访问很慢。 If it was an array then random access would be fast.
如果它是一个数组,那么随机访问将很快。
In addition to removing the unnecessary declarations of smallest
, you are also resetting smallest
to the size of the LinkedList every time that you iterate through with your int smallest = integers.size();
除了删除不必要的
smallest
声明之外,您还需要在每次使用int smallest = integers.size();
进行迭代时,将smallest
重置为LinkedList的大小int smallest = integers.size();
code. 码。 You should remove this along with the code at the end that states:
您应该在代码末尾删除此内容,并指出:
int smallest = integers.get(Oedipus);
System.out.println("The smallest element is: " + smallest);
All this does is it redefines smallest
as the last element. 这一切都是将
smallest
的元素重新定义为最后一个元素。
Your approach is somewhat strange. 您的方法有些奇怪。 Also you should consider the scope of variables.
您还应该考虑变量的范围。 the "smallest" variables that you have through your code are different.
您的代码中的“最小”变量是不同的。
Analyze the following redefined function 分析以下重新定义的函数
public static void main(String[] args) {
// TODO code application logic here
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);
}
int smallest = integers.get(0);
System.out.println(integers); //Prints out that list.
for( int Oedipus =1; Oedipus <integers.size()-1; Oedipus++)
{
if(integers.get(Oedipus) < smallest )
{
//int smallest = integers.get(Oedipus);
smallest = integers.get(Oedipus);
}
}
System.out.println("The smallest element is: " + smallest);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.