Java项目-重写toString()方法并返回字符串
我的类已经开始处理队列,我们的指令是重写toString()方法并返回字符串。如果不使用默认方法,我不知道如何将数组转换为字符串形式[element,element,element]以返回。这是到目前为止我的代码。我的队列工作正常,不需要任何输入…只需要返回toString()方法Java项目-重写toString()方法并返回字符串,java,string,return,tostring,Java,String,Return,Tostring,我的类已经开始处理队列,我们的指令是重写toString()方法并返回字符串。如果不使用默认方法,我不知道如何将数组转换为字符串形式[element,element,element]以返回。这是到目前为止我的代码。我的队列工作正常,不需要任何输入…只需要返回toString()方法 package edu.ben.cis205; public class MyQueue { //Create array public int[] array = new int[10];
package edu.ben.cis205;
public class MyQueue {
//Create array
public int[] array = new int[10];
//Create variables to track number of elements and pointer positions
private int first = 0;
private int last = 0;
private int numberOfElements = 0;
public int peek() {
//Returns first element in array
if (numberOfElements > 0)
return array[first];
else {
System.out.println("No integers in array.");
return 0;
}
}
public boolean add(int inputNumber) {
//Adds input to array
//Checks for room at back of array
if (numberOfElements < array.length && last < array.length) {
array[last] = inputNumber;
last++;
numberOfElements++;
return true;
//Checks for room at front of array
} else if (numberOfElements < array.length && last == array.length) {
last = 0;
array[last] = inputNumber;
last++;
numberOfElements++;
return true;
} else {
return false;
}
}
public int getSize() {
//Returns number of elements in array
return numberOfElements;
}
public boolean isFull() {
//Returns true if full
if (numberOfElements == array.length)
return true;
else
return false;
}
public boolean isEmpty() {
//Returns true if array is empty
if (numberOfElements == 0)
return true;
else
return false;
}
public int remove() {
//Removes element at front of array
//Checks for elements and moves pointer to next array position
if (numberOfElements > 0 && first < array.length) {
int returnValue = array[first];
first++;
numberOfElements--;
return returnValue;
//Checks for elements and moves pointer to front if at final position
} else if (numberOfElements > 0 && first == array.length) {
first = 0;
int returnValue = array[first];
first++;
numberOfElements--;
return returnValue;
//Returns an int value of 0 if array is empty
} else {
return 0;
}
}
public int getCapacity() {
//Returns array size
return array.length;
}
public int getRemainingCapacity() {
//Returns remaining spaces in array
return array.length - numberOfElements;
}
//ERROR IN METHOD
public String toString() {
//Int establishes where to start in the array.
int arrayPointer = first;
//New array established to put queue in order.
int stringArray[] = new int[numberOfElements];
//This code reconstructs the queue in the correct order
if (numberOfElements != 0) {
for (int i = 0; i < numberOfElements; i++) {
stringArray[i] = array[arrayPointer];
if (arrayPointer < (array.length - 1))
arrayPointer++;
else
arrayPointer = 0;
}
} else
return null;
// ??? Do not know how to change the new array (stored now in the correct order) into a String return...
}
}
包edu.ben.cis205;
公共类MyQueue{
//创建数组
公共int[]数组=新int[10];
//创建变量以跟踪元素数量和指针位置
私有int first=0;
private int last=0;
私有int numberOfElements=0;
公共int peek(){
//返回数组中的第一个元素
如果(numberOfElements>0)
返回数组[第一];
否则{
System.out.println(“数组中没有整数”);
返回0;
}
}
公共布尔加法(int inputNumber){
//将输入添加到数组中
//检查阵列后面的空间
if(numberOfElements0&&first0&&first==array.length){
第一个=0;
int returnValue=array[first];
第一++;
元素数--;
返回值;
//如果数组为空,则返回int值0
}否则{
返回0;
}
}
公共int getCapacity(){
//返回数组大小
返回数组长度;
}
公共整数getRemainingCapacity(){
//返回数组中的剩余空格
返回array.length-numberOfElements;
}
//方法错误
公共字符串toString(){
//Int确定数组中的起始位置。
int arrayPointer=第一个;
//已建立新数组以将队列按顺序排列。
int stringArray[]=新的int[numberOfElements];
//这段代码按照正确的顺序重建队列
if(numberOfElements!=0){
for(int i=0;i
使用
以[item1,item2,item3]表示形式获取数组