Java 使用类查找数据结构中的错误
当我向ArrayList数据结构类添加元素时,我需要使用ReturnObjectImpl类来基本上查找错误(这是一个大学作业) 我不确定如何让ArrayList类中的函数返回ReturnObject。我需要某种方式将所有内容传递给ReturnObject,检查是否存在错误(我也不知道如何做),然后提供错误消息或对象Java 使用类查找数据结构中的错误,java,return-value,return-type,Java,Return Value,Return Type,当我向ArrayList数据结构类添加元素时,我需要使用ReturnObjectImpl类来基本上查找错误(这是一个大学作业) 我不确定如何让ArrayList类中的函数返回ReturnObject。我需要某种方式将所有内容传递给ReturnObject,检查是否存在错误(我也不知道如何做),然后提供错误消息或对象 public interface ReturnObject { /** * Returns whether there has been an error
public interface ReturnObject {
/**
* Returns whether there has been an error
* @return whether there has been an error
*/
public boolean hasError();
/**
* Returns the error message.
*
* This method must return NO_ERROR if and only if
* {@hasError} returns false.
*
* @return the error message
*/
public ErrorMessage getError(); //Changes the return to a String has in the interface ErrorMessage is throwing an error - if marks are deducted, please discuss with me
/**
* Returns the object wrapped in this ReturnObject, i.e. the
* result of the operation if it was successful, or null if
* there has been an error.
*
* Note that the output of this method must be null if {@see
* hasError} returns true, but the opposite is not true: if
* {@see hasError} returns false, this method may or may not
* return null.
*
* @return the return value from the method or null if there has been an error
*/
public Object getReturnValue();
}
然后是类本身(目前尚未完成):
最后是ArrayList类
public class ArrayList implements List{
public static final int CAPACITY=16;
private int size = 0;
private Object[] data;
//constructors
public ArrayList() {
data = new Object[CAPACITY];
} //Constructs arraylist with default capacity
public ArrayList(int capacity) { // Constructs arraylist with given capacity
data = new Object[capacity];
System.out.println("Created an ArrayList of capacity " + capacity);
}
public boolean isEmpty(){
return (size == 0);
}
public int size(){
System.out.println("The ArrayList is not full, but currently has " + size + " indexs");
return size;
}
public ReturnObject get(int index){
return null; //INCOMPLETE
}
public ReturnObject remove(int index){
return null;
}
public ReturnObject add(int index, Object item){
if(index <= size && index < data.length){
for (int x = size-1; x >= index; x--){
data[x+1] = data[x];
size++;
}
data[index] = item;
System.out.println("Added to array at " + index);
}
return null;
}
public ReturnObject add(Object item){
if (data[0] == null){
data[0] = item;
}
//int adding = size + 1;
data[size] = item;
System.out.println("Added item to index " + size);
size++;
return null;
}
//added - but DELETE BEFORE SUBMITTING
public void printAll(){
for(int x = 0; x < data.length; x++){
System.out.println(data[x]);
}
}
}
公共类ArrayList实现列表{
公共静态最终int容量=16;
私有整数大小=0;
私有对象[]数据;
//建设者
公共数组列表(){
数据=新对象[容量];
}//使用默认容量构造arraylist
公共ArrayList(int-capacity){//使用给定的容量构造ArrayList
数据=新对象[容量];
System.out.println(“创建了容量的数组列表”+容量);
}
公共布尔值为空(){
返回(大小==0);
}
公共整数大小(){
System.out.println(“ArrayList未满,但当前有“+size+”索引”);
返回大小;
}
public ReturnObject get(int索引){
返回null;//不完整
}
公共返回对象删除(int索引){
返回null;
}
public ReturnObject add(int索引,对象项){
if(index=index;x--){
数据[x+1]=数据[x];
大小++;
}
数据[索引]=项目;
System.out.println(“添加到“+索引处的数组中”);
}
返回null;
}
公共返回对象添加(对象项){
如果(数据[0]==null){
数据[0]=项目;
}
//整数相加=大小+1;
数据[大小]=项目;
System.out.println(“添加到索引的项目”+大小);
大小++;
返回null;
}
//添加-但在提交前删除
public void printAll(){
对于(int x=0;x
简言之,我有两个问题:
1.returnObjectImpl中的错误检查函数应该是什么样的
2.更重要的是,我应该如何将公共ReturnObject add(Object item)的结果从ArrayList类发送到ReturnObjectImpl。如果
List
是java.util.List
接口,那么这里有一些严重错误:
ArrayList
不是通用接口。(如果您是为Java1.4.x编写的,那也没关系。但是Java1.4.x在10年前就退役了!)ArrayList
方法与java.util.List
API不兼容。例如,add
应返回一个boolean
,get
应返回元素类型(或对象,如果忽略泛型),而不是其他类型
List
是个坏主意。对于ArrayList
,同上。您不应该像这样“借用”标准类和接口的名称。这会给你带来麻烦
回答您的问题: 1)
returnObjectImpl
中的错误检查函数应该是什么样的
没有returnObjectImpl
方法。并且错误检查不属于ReturnObjectImpl
类。该类(根据其名称和API设计)只是一个表示值或错误条件的“持有者”。实际的错误检查代码属于数组列表类;e、 像这样的东西
if (/* some error */) {
return new ReturnObjectImpl(/* some error message */);
}
2) 更重要的是,我应该如何将公共ReturnObject add(Object item)的结果从ArrayList
类发送到ReturnObjectImpl
显然,您必须重新设计ReturnObjectImpl
构造函数才能实现这一点
P>意见:我认为某人可能已经陷入了“不使用例外”的心态,这会影响到一些学习程序(如C或C++)的人。在不讨论异常是“好”还是“坏”的情况下,事实仍然是它们是Java语言不可分割的一部分,并且它们在整个Java语言和类库中都得到了一致的使用。你无法避免它们,如果你尝试,你会伤害自己 如果这是您的设计,并且您真的希望避免如此多的异常。。。您应该使用不同的编程语言2
1-这并不是故意贬低,但我担心“避免异常”的想法很少(如果有的话)会在Java生产率或可维护性方面产生好的结果
2-我想起了“真正的程序员可以用任何语言编写FORTRAN”这句话。List实际上是一个我没有包括在这里的接口。谢谢你的帮助。
if (/* some error */) {
return new ReturnObjectImpl(/* some error message */);
}
if (/* not an error */) {
return new ReturnObjectImpl(/* the value */);
}