需要帮助为这个Java程序开发一个合适的打印方法吗
这个程序从用户输入中获取整数,并将它们放入一个集合中。然后它先打印正值,然后打印负值,不打印重复的数字。一旦用户输入0,它将停止请求输入。代码如下:需要帮助为这个Java程序开发一个合适的打印方法吗,java,Java,这个程序从用户输入中获取整数,并将它们放入一个集合中。然后它先打印正值,然后打印负值,不打印重复的数字。一旦用户输入0,它将停止请求输入。代码如下: public class Intcoll2 { private int[] c; private int[] d; private int howmany = 0; public Intcoll2() { c = new int[500]; } public Intcoll2(int i
public class Intcoll2
{
private int[] c;
private int[] d;
private int howmany = 0;
public Intcoll2()
{
c = new int[500];
}
public Intcoll2(int i)
{
c = new int[i]
}
public void insert(int i)
{
if (i > 0)
{
int j = 0;
while ((j <= howmany) && (c[j] != i)) j++;
if (j == howmany)
{
if (j == c.length - 1)
{
d = new int[2*c.length];
for (int k = 0; k<c.length; i++){
d[k] = c[k];
}
c = d;
}
c[j] = i; c[j + 1] = 0;
}
howmany++;
}
}
public int get_howmany()
{
int j=0, howmany=0;
while (c[j]!=0) {howmany++; j++;}
return howmany;
}
公共类Intcoll2
{
私有int[]c;
私有int[]d;
私有整数多少=0;
公共Intcoll2()
{
c=新整数[500];
}
公共Intcoll2(int i)
{
c=新整数[i]
}
公共空白插入(int i)
{
如果(i>0)
{
int j=0;
虽然((j一个你可能不想寻找的答案,但仍然是唯一你应该关心的真实的答案
您的问题不是代码中的某个地方隐藏了一个bug。问题是您的代码令人困惑超出了限制:
- 不要使用单字符变量名
- 接受int的构造函数…创建一个空数组
- 使用数组时不要说“collection”
- 不要给字段和局部变量指定相同的名称
严肃地说:理解这种混乱主要是复杂和困难的,因为您编写的代码很难阅读
现在,您要求其他人调试如此复杂的代码,以至于您(创建它的作者!)一开始就不理解
相反,你可以把这整件事都扔掉。然后慢慢地再写一遍;但写的方式一点也不会让读者感到困惑。这是一个你可能没有寻找的答案,但仍然是你应该关心的唯一真正的答案
您的问题不是代码中的某个地方隐藏了一个bug。问题是您的代码令人困惑超出了限制:
- 不要使用单字符变量名
- 接受int的构造函数…创建一个空数组
- 使用数组时不要说“collection”
- 不要给字段和局部变量指定相同的名称
严肃地说:理解这种混乱主要是复杂和困难的,因为您编写的代码很难阅读
现在,您要求其他人调试如此复杂的代码,以至于您(创建它的作者!)一开始就不理解
相反,你可以把这整件事扔掉。然后慢慢地再写一遍;但要用一种不会让读者感到困惑的方式。我看了你的课,并以更清晰的方式重写了它。我没有测试它,但我相信它能工作。你可以检查它,希望你能理解发生了什么。希望这能有所帮助
public class IntCollection2 {
private int[] collection; // A large allocation, not neccessarily filled up.
private int currentSize; // The number of spots currently filled in the collection.
public IntCollection2() {
collection = new int[500];
currentSize = 0;
}
public IntCollection2(int size) {
collection = new int[size];
currentSize = 0;
}
/**
* Inserts a new element into the internal array. If the current array is filled up,
* a new array double the size of the current one is allocated.
* @param element An int to insert into the collection. Must not be '0'.
* @return True if the element was successfully inserted, false if the element was
* equal to '0' and was ignored.
*/
public boolean insert(int element) {
if (element != 0) {
if (currentSize < collection.length - 1) {
collection[currentSize] = element;
} else {
int[] newCollection = new int[collection.length * 2];
for (int i = 0; i < currentSize; i++) {
newCollection[i] = collection[i];
}
newCollection[currentSize] = element;
collection = newCollection;
}
currentSize++;
return true;
} else {
return false;
}
}
/**
* Not actually necessary because the class automatically updates its currentSize
* every time a new element is inserted.
* @return The current number of filled spots in the internal array.
*/
public int getCurrentSize() {
int size = 0;
for (int i = 0; i < collection.length && collection[i] != 0; i++) {
size++;
}
return size;
}
/**
* Prints out all the elements currently in the collection, one on each line.
*/
public void print() {
System.out.println();
for (int i = 0; i < currentSize; i++) {
System.out.println(collection[i]);
}
}
}
公共类IntCollection2{
private int[]collection;//一个大的分配,不需要填充。
private int currentSize;//集合中当前填充的点数。
公共IntCollection2(){
集合=新整数[500];
currentSize=0;
}
公共IntCollection2(整数大小){
集合=新整数[大小];
currentSize=0;
}
/**
*将新元素插入内部数组。如果当前数组已满,
*将分配一个比当前数组大一倍的新数组。
*@param元素是要插入到集合中的int。不能是“0”。
*@如果成功插入元素,则返回True;如果成功插入元素,则返回false
*等于“0”,已被忽略。
*/
公共布尔插入(int元素){
如果(元素!=0){
如果(currentSize
仅供参考:这个类只是按顺序打印集合中的每个元素。您提到了打印正值和负值,但我把这个留给您
编辑:我猜你是编程新手,所以我只想明确什么是集合。数组是元素的有序列表。创建数组时,计算机会留出一点内存来精确保存你告诉它的元素数。你不能更改现有数组的大小。集合基本上是数组的包装器。它生成的数组比容纳其元素所需的数组大,当数组满时,它会分配一个新的、更大的数组来容纳更多元素。我查看了您的类,并以更清晰的方式重写了它。我没有测试它,但我相信它可以工作。您可以检查它希望你能明白发生了什么。希望这能有所帮助
public class IntCollection2 {
private int[] collection; // A large allocation, not neccessarily filled up.
private int currentSize; // The number of spots currently filled in the collection.
public IntCollection2() {
collection = new int[500];
currentSize = 0;
}
public IntCollection2(int size) {
collection = new int[size];
currentSize = 0;
}
/**
* Inserts a new element into the internal array. If the current array is filled up,
* a new array double the size of the current one is allocated.
* @param element An int to insert into the collection. Must not be '0'.
* @return True if the element was successfully inserted, false if the element was
* equal to '0' and was ignored.
*/
public boolean insert(int element) {
if (element != 0) {
if (currentSize < collection.length - 1) {
collection[currentSize] = element;
} else {
int[] newCollection = new int[collection.length * 2];
for (int i = 0; i < currentSize; i++) {
newCollection[i] = collection[i];
}
newCollection[currentSize] = element;
collection = newCollection;
}
currentSize++;
return true;
} else {
return false;
}
}
/**
* Not actually necessary because the class automatically updates its currentSize
* every time a new element is inserted.
* @return The current number of filled spots in the internal array.
*/
public int getCurrentSize() {
int size = 0;
for (int i = 0; i < collection.length && collection[i] != 0; i++) {
size++;
}
return size;
}
/**
* Prints out all the elements currently in the collection, one on each line.
*/
public void print() {
System.out.println();
for (int i = 0; i < currentSize; i++) {
System.out.println(collection[i]);
}
}
}
公共类IntCollection2{
private int[]collection;//一个大的分配,不需要填充。
私有int-currentSize;