簡體   English   中英

通過其變量和clone()之一對自制類的數組列表進行排序將不起作用

[英]sorting an arraylist of a self-made class by one of its variables and clone() will not work

好; 我正在為學校做一個項目,它的游戲類似於落沙。 但是,為了使重力起作用,我必須按照從下到上的位置(沙子的y變量)對沙子進行排序(此方法應對沙子進行排序); 盡管我無法使.clone()正常工作,並且我無法以其他任何方式硬拷貝我知道的方式。 所以我不知道如何用能做他們所​​說的話來代替這段代碼中的所有注釋。

解釋我希望它如何工作; 我希望它一個接一個地從世界中刪除元素,而我將它們按排序放置。

   public void sort(){
  //method to sort elements by y value
     ArrayList<sand> sorted=new ArrayList<sand>();
     if(world.size()!=0){
        //code to take 0 from world and place it into sorted at 0
        while(world.size()>0){
           boolean check=true;
           for(int i=0;i<sorted.size();i++){
              if(world.get(0).y<sorted.get(i).y){
                 //code to take 0 from world and place it into sorted at i
                 check=false;
              }
           }
           if(check){
              //code to take 0 from world and place it at the end
           }
        }
     }
     //code to make sorted the contents of world
  }

我與克隆得到的錯誤是:

awesomesand.java:48: clone() has protected access in java.lang.Object
            sand a=world.get(0).clone();

而且,是的,世界是沙子類型的。


編輯

現在我在克隆時出錯。

awesomesand.java:48: incompatible types
found   : java.lang.Object
required: awesomesand.sand
            sand a=world.get(0).clone();
                                     ^

如果我正確理解這一點,我認為您只想對數組排序。 如果是這樣,我認為克隆並不是真正必要的。

也許您可以嘗試使用Comparator和Collections.sortList()方法將世界數組按正確的順序排序(該順序由Comparator的實現確定)。 如果需要使用其他數組而不是world,則可以簡單地首先使用適當的ArrayList副本構造函數或System.arraycopy創建world的副本,然后對該數組進行排序。

您正在獲取克隆異常,因為它在Object具有受保護的訪問。 但是,如果調用Collections的標准排序機制,則對ArrayList進行排序並不需要clone()

至於為什么出現克隆錯誤,一個類必須使用公共訪問覆蓋clone() 這是為了確保您可以處理班級的細節:

public class Sand implements Cloneable {
    public Object clone() throws CloneNotSupportedException {
        Sand copy = (Sand) super.clone();
        // Sand specific copying done here
        return copy;
    }
}

但是,更簡單,更有效並且更可能正確的是,直接對沙子對象集進行排序。 在這里,我定義了一個可能的Sand版本,並展示了Collections.sort的用法:

public class Sand {
    public int x;
    public int y;
}

public class SandComparator implements Comparator<Sand> {
    public int compare(Sand s1, Sand s2) {
        // reverse these to sort in the opposite order
        return s1.y - s2.y;
    }
    public boolean equals(Object o) {
        return o instanceof SandComparator;
    }
}

public class App {
    ArrayList<Sand> world;
    ...
    public void sort() {
        Collections.sort(world, new SandComparator());
    }
}

可能是因為“他們”想強迫您考慮是需要對象的淺表副本還是深表副本。 在您的類中,您需要實現clone()方法。 您還需要實現Cloneable接口:

public class sand implements Cloneable
{
    public Object clone() throws CloneNotSupportedException
    {
        return super.clone();
    }
}

這將創建一個淺表副本。

免責聲明:自從我積極使用Java已經有很多年了,但是我希望這會有所幫助。

PS:您確實應該為類名稱使用PascalCasing :)

除了bm212指出的內容(使用Collections.sort )外,讓我指出您的排序方法不起作用。 您只需遍歷列表,然后將所有元素都小於您的數據透視表的開頭,將所有其他元素末尾放置,這不足以得到排序的列表。

如果您真的想實現自己的排序,那么最好使用簡單的插入或冒泡排序:

http://en.wikipedia.org/wiki/Insertion_sort

http://en.wikipedia.org/wiki/Bubble_sort

哦,忘了使用clone ,我真的懷疑您是否需要它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM