我正在尝试创建一个程序,用户可以将整数添加到链表中,然后计算最大数字是什么,最小数字是什么,数字总和是多少等等。我对此遇到了一些麻烦,我发现链接列表非常令人困惑,我希望有人可以帮我弄清楚我做错了什么。
import java.util.*;
public class UnorderedLinkedListInt extends LinkedListIntClass {
int size=0;
LinkedList<Integer> list;
public boolean isEmptyList(){
if(size==0){
return true;
}else{return false;}
}
public void initializeList(){
list = new LinkedList<Integer>();
}
public void print(){
for(int x : list){
System.out.printf("%s ",x);
}
}
public int length(){
return size;
}
public int front(){
return list.getFirst();
}
public int back(){
return list.getLast();
}
public void insertFirst(int newItem){
list.addFirst(newItem);
}
public void insertLast(int newItem){
list.addLast(newItem);
}
public boolean search(int searchItem){
return list.contains(searchItem);
}
public void deleteNode(int deleteItem){
list.remove(deleteItem);
}
public int findSum(){
int sum =0;
for(int x:list){
sum=sum+x;
}
return sum;
}
public int findMin(){
int min =list.get(0);
for(int i=1;i<list.size();i++){
if(list.get(i)<min){
min=list.get(i);
}
}
return min;
}
public int findMax(){
int max=list.get(0);
for(int i=1;i<list.size();i++){
if(list.get(i)>max){
max=list.get(i);
}
}
return max;
}
}
这是我用来测试代码的客户端方法。
import java.util.*;
public class ClientUnorderedLinkedListInt {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
UnorderedLinkedListInt intList = new UnorderedLinkedListInt();
UnorderedLinkedListInt tempList;
int num;
System.out.println("Enter integers (999 to stop)");
num = input.nextInt();//valid??
while (num != 999) {
intList.insertLast((Integer) num);
num = input.nextInt();//valid??
}
System.out.print("\nTesting .insertLast and .print. The original list is: ");
intList.print();
System.out.println("\nTesting .length. The length of the list is: " + intList.length());
if (!intList.isEmptyList()) {
System.out.println("Testing .front. First element/list: " + intList.front());
System.out.println("Testing .back. Last element/list: " + intList.back());
}
System.out.println("Testing .sum. The sum of data in all nodes is: " + intList.findSum());
System.out.println("Testing .min. The smallest data in all nodes is: " + intList.findMin());
System.out.print("Testing .search. Enter the number to search for/list: ");
num = input.nextInt(); //valid??
if (intList.search(num))
System.out.println(num + " found in this list.");
else
System.out.println(num + " is not in this list.");
System.out.print("Testing .remove. Enter the number to be deleted from list: ");
num = input.nextInt();//valid??
intList.deleteNode(num);
System.out.print("Testing .toString. After deleting " + num + ", the list is: " + intList);
System.out.println("\nThe length of the list after delete is: " + intList.length());
}
}
答案 0 :(得分:2)
添加或删除项目时不会更新size变量,您应该尽可能使用List类的现有方法(size(),isEmpty()等)。
你也可能是auto-boxing的受害者。因为您的列表包含整数(对象),所以您需要验证是否正在调用List类的适当方法,具体取决于您是传递索引还是“值”对象。 remove(int)方法经常使人们绊倒,因为它已经过载,可以使用原始int进行索引,也可以使用整数对象作为列表中的项目。
答案 1 :(得分:2)
您必须调用initializeList()或创建一个构造函数来创建链接列表并使其保持正确的默认状态。
public UnorderedLinkedListInt() {
super();
initializeList();
}
答案 2 :(得分:0)
除了这里的其他两个答案(到目前为止),我将在客户类的倒数第二行添加,
System.out.print("Testing .toString. After deleting " + num + ", the list is: " + intList);
您的代码只是打印出Testing .toString. After deleting 1, the list is: UnorderedLinkedListInt@4a5a12f5
,我想您可以看到为什么这可能是不受欢迎的。
尝试编写格式化列表的toString()
方法;那么你包含的代码就可以了。