亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

對象列表 Java 任務

對象列表 Java 任務

翻閱古今 2022-01-12 16:49:06
我已經獲得了以下主要方法,并且必須為 ObjectList 類編寫代碼。我應該推斷 ObjectList 類的必要功能并自己編寫該類,但是我不確定我需要做什么才能實現此功能。非常感謝任何幫助理解這一點。這是給我的代碼:   ObjectList ol = new ObjectList(3);   String s = "Im Happy";   Dog d = new Dog();   DVD v = new DVD();   Integer i = 1234;   System.out.println(ol.add(s));   System.out.println(ol.add(d));   System.out.println(ol.add(v));   System.out.println(ol.add(i));   ol.remove(0);   System.out.println(ol.add(i));   System.out.println("Is the list full? "+ isFull());    System.out.println("Is the list empty? "+ isEmpty());   System.out.println("Total number of objects in the list: " + getTotal());   Object g = ol.getObject(1);   g.bark();
查看完整描述

2 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

這是很簡單只需要創建的列表,Object使用類型ArrayList或LinkedList在ObjectList類并實現功能如下


public class ObjectList{

private ArrayList<Object> objects;

public ObjectList(int size)

{

    objects = new ArrayList<Object>(size);

}


public String add (Object object)

{

    objects.add(object);

    //anything you would like to return I'm just returning a string

    return "Object Added";

}


public void remove (int index)

{

    objects.remove(index);

}


public boolean isEmpty()

{

    return objects.isEmpty();

}


public int getTotal()

{

    return objects.size();

}


public Object getObject(int index)

{

    return objects.get(index);

}

}

在isFull()沒有必要的,因為ArrayList大小可動態變化。您可以使用簡單的數組代替ArrayList然后實現該isFull()函數。


此外,當使用 getgetObject()函數獲取對象時,您需要在使用該函數之前將其轉換為正確的類型。在您的代碼中g.bark()不起作用,因為Object沒有樹皮功能


Object g = ol.getObject(1);


//This can give a runtime error if g is not a Dog

//Use try catch when casting

Dog d = (Dog)g;


d.bark();

編輯


isFull()如果使用數組而不是ArrayList為了簡單起見,請使用該ArrayList版本,這就是您將如何實現和其他功能的方式


 public class ObjectList{

 private Object[] objects;

 private int size = 0;

 private int currentIndex = 0;



public ObjectList(int size)

{

    this.size = size;

    objects = new Object[size];

}


 private boolean isFull() {

    if(currentIndex == size)

        return true;

    else

        return false;

 }


 public String add (java.lang.Object object)

{

    if ( ! isFull() ) {

        objects[currentIndex] = object;

        currentIndex++;

        return "Object added";

    }

    return "List full : object not added";


}


public void remove (int index)

{

    if( !isEmpty() ) {

        //shifting all the object to the left of deleted object 1 index to the left to fill the empty space

        for (int i = index; i < size - 1; i++) {

            objects[i] = objects[i + 1];

        }

        currentIndex--;

    }

}


public boolean isEmpty()

{

    if(currentIndex == 0)

        return true;

    else

        return false;

}


public int getTotal()

{

    return currentIndex;

}


public java.lang.Object getObject(int index)

{

    if(index < currentIndex)

     return objects[index];

    else

        return  null;

}

}



查看完整回答
反對 回復 2022-01-12
?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

您似乎想要實現的是“擴展” ArrayList的功能并創建自定義對象列表。您可以做的是創建一個擴展 ArrayList 的類并定義/覆蓋您想要的任何其他方法。


public class ObjectList extends ArrayList<Object> {

//constructor with initial capacity

private int length;

public ObjectList(int size){

   super(size);

   this.length= size;

}


public Object getObject(int index){

    return this.get(index);

}

}

現在您擁有從 ArrayList 類和getObject方法繼承的添加和刪除函數。


關于isFull方法,您可以檢查 ObjectList 類的大小是否等于它被實例化的大小


if(this.size() == this.length){

return true

}

return false;

并獲得總計


public int getTotal(){

return this.size();

}


查看完整回答
反對 回復 2022-01-12
  • 2 回答
  • 0 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號