gpt4 book ai didi

Java 计数功能不起作用。控制台应用程序数字数组

转载 作者:行者123 更新时间:2023-12-01 23:26:37 25 4
gpt4 key购买 nike

我的 int count(int i) 函数遇到问题:该函数应该计算列表中出现的数字的次数。例如有一个数字数组 3 4 4 1 3 2 1这是它应该显示的内容:

Item 0  count = 0
Item 1 count = 2
Item 2 count = 1
Item 3 count = 2
Item 4 count = 2
Item 5 count = 0

class Bag
{
private Node first; //dummy header node

// Initializes the list to empty creating a dummy header node.
public Bag()
{
first = new Node();
}

// Returns true if the list is empty, false otherwise
public boolean isEmpty()
{
return (first.getNext() == null);
}

// Clears all elements from the list
public void clear()
{
first.setNext(null);
}

// Returns the number of item in the list
public int getLength()
{
int length = 0;
Node current = first.getNext();
while (current != null)
{
length++;
current = current.getNext();
}
return length;
}

// Prints the list elements.
public String toString()
{
String list = "";
Node current = first.getNext();
while (current != null)
{
list += current.getInfo() + " ";
current = current.getNext();
}
return list;
}

// Adds the element x to the beginning of the list.
public void add(int x)
{
Node p = new Node();
p.setInfo(x);
p.setNext(first.getNext());
first.setNext(p);
}

// Deletes an item x from the list. Only the first
// occurrence of the item in the list will be removed.
public void remove(int x)
{
Node old = first.getNext();
Node p = first;

//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x)
found = true;
else
{
p = old;
old = p.getNext();
}
}

//if x is in the list, remove it.
if (found)
p.setNext(old.getNext());
}

//public int count(int item) {
// int count = 0;

//for(int i = 0; i < length; i++) {
// if(bag[i] == item) {
// count++;
// }
//}

// return count;
//}


// Inner class Node.
private class Node
{
private int info; //element stored in this node
private Node next; //link to next node

// Initializes this node setting info to 0 and next to null
public Node()
{
info = 0;
next = null;
}

// Sets the value for this node
public void setInfo(int i)
{
info = i;
}

// Sets the link to the next node
public void setNext(Node lnk)
{
next = lnk;
}

// Returns the value in this node
public int getInfo()
{
return info;
}

// Returns the link to the next node
public Node getNext()
{
return next;
}
}
}

// Class implementing a linked list.
class LinkedList
{
private Node first; //dummy header node

// Initializes the list to empty creating a dummy header node.
public LinkedList()
{
first = new Node();
}

// Returns true if the list is empty, false otherwise
public boolean isEmpty()
{
return (first.getNext() == null);
}

// Clears all elements from the list
public void clear()
{
first.setNext(null);
}

// Returns the number of item in the list
public int getLength()
{
int length = 0;
Node current = first.getNext();
while (current != null)
{
length++;
current = current.getNext();
}
return length;
}

// Prints the list elements.
public String toString()
{
String list = "";
Node current = first.getNext();
while (current != null)
{
list += current.getInfo() + " ";
current = current.getNext();
}
return list;
}

// Adds the element x to the beginning of the list.
public void add(int x)
{
Node p = new Node();
p.setInfo(x);
p.setNext(first.getNext());
first.setNext(p);
}

// Deletes an item x from the list. Only the first
// occurrence of the item in the list will be removed.
public void remove(int x)
{
Node old = first.getNext();
Node p = first;

//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x)
found = true;
else
{
p = old;
old = p.getNext();
}
}

//if x is in the list, remove it.
if (found)
p.setNext(old.getNext());
}

// Returns the element at a given location in the list
public int get(int location)
{
int item = -1;
int length = getLength();

if (location <1 || location > length)
System.out.println("\nError: Attempted get location out of range.");
else
{
int n = 1;
Node current = first.getNext();
while (n < location)
{
n++;
current = current.getNext();
}
item = current.getInfo();
}
return item;
}

/************************************************************************
Students to complete the following two methods for the LinkedList class
***********************************************************************/
// Adds item to the end of the list
public void addEnd(int item)
{
Node currentPos = new Node();
Node newPos = new Node(); //create a new node
newPos.setInfo(item); //load the data
currentPos = first;

while(currentPos.getNext() !=null)
{
currentPos = currentPos.getNext();
}
currentPos.setNext(newPos);
}

// Replaces the info in the list at location with item
public void replace(int location, int item)
{
Node currentPos = new Node();
Node prevPos = new Node();
Node nextPos = new Node();
int length = getLength();

if (location <1 || location > length)
System.out.println("\nError: Attempted get location out of range.");
else
{
prevPos = first;
for(int i=0; i < location-1; i++)
{
prevPos = prevPos.getNext();
}
currentPos = prevPos.getNext();
nextPos = currentPos.getNext();
Node newPos = new Node();
newPos.setInfo(item);
newPos.setNext(nextPos);
prevPos.setNext(newPos);
}
}

// Inner class Node.
private class Node
{
private int info; //element stored in this node
private Node next; //link to next node

// Initializes this node setting info to 0 and next to null
public Node()
{
info = 0;
next = null;
}

// Sets the value for this node
public void setInfo(int i)
{
info = i;
}

// Sets the link to the next node
public void setNext(Node lnk)
{
next = lnk;
}

// Returns the value in this node
public int getInfo()
{
return info;
}

// Returns the link to the next node
public Node getNext()
{
return next;
}
}
}

public class Lab2B2
{
public static void main(String args[])
{
Bag intBag = new Bag();

for (int i =0; i < 10; i++)
{
int info = (int)(Math.random()*10);
intBag.add(info);
}

// Before List
System.out.print("List creation before: " + intBag);

// Counts the # of occurrences of item in the bag

//System.out.println("\nCount the number of occurrences:");
//for(int i = 0; i <= 10; i++)
//{
//System.out.println("Item " + i + " count = " + list.count(i));
//}

// Returns the number of items in the bag
System.out.print("\nLength of List: " + intBag.getLength());

// Adds an item to the bag
intBag.add(9);
System.out.print("\nList creation - Add(9): " + intBag);

// Removes an item from the bag, all occurrences of item in the bag
intBag.remove(8);
System.out.print("\nList creation - Remove(8): " + intBag);

// Removes all of the items from the bag
intBag.clear();
System.out.print("\nList creation - Clear(): " + intBag);

// Determines whether the bag is empty

}
}

最佳答案

您正在管理一个 LinkedList。您需要做的是运行节点并检查它们是否等于您正在检查的项目。在这种情况下,您可以增加计数。该代码充满了如何遍历列表的示例。getLength() 方法是一个非常好的开始,因为它具有遍历所有元素的代码。只需稍加修改,您就可以得到您想要的。

public int count(int item) {
int count = 0;
Node current = first.getNext();
while (current != null)
{
if (current.getInfo()==item) {
count++;
}
current = current.getNext();
}
return count;
}

关于Java 计数功能不起作用。控制台应用程序数字数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19883769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com