Autor Beitrag
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Mi 20.05.09 12:13 
Hallo zusammen,

unser Dozent in Algorithmen & Datenstrukturen hat uns die Aufgabe gegegeben, ein Testprogramm für offenes Hashing (mit direkter und separater Verkettung) und geschlossenes Hashing (lineares Sondieren, quadratisches Sondieren und Double Hashing) zu implementieren.
Ich habe die beiden abstrakten Klassen HashTable und Bucket, davon erben jeweils DirectChainedHasTable und DirectChainBucket.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
package hashing;

import java.util.ArrayList;

public abstract class HashTable {
  
  protected ArrayList<Bucket> bucketList;
    
  public int getLength() {
    return this.bucketList.size();
  }

  
}

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
package hashing;

public abstract class Bucket {
  
  protected boolean occupied;
  protected int id;
  
  public abstract String toString();

}

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
package hashing;

import java.util.ArrayList;

public class DirectChainedBucket extends Bucket {

  private ArrayList<SuperString> objectList;

  @Override
  public String toString() {
    return "DirectChainedBucket, length: " + this.objectList.size();
  }
}

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
package hashing;

import java.util.ArrayList;

public class DirectChainedHashTable extends HashTable {

  public DirectChainedHashTable() {
    this.bucketList = new ArrayList<DirectChainedBucket>();
    
  }
  
}

Eclipse meldet in der Klasse DirectChainedHashTable in der Instanziierung der ArrayList folgendes:

Description Resource Path Location Type
Type mismatch: cannot convert from ArrayList<DirectChainedBucket> to ArrayList<Bucket> DirectChainedHashTable.java Hashing/src/hashing line 8 Java Problem

Ich dachte bis jetzt immer, wenn ich irgendwo ein Feld als Objekt von einer Mutterklasse angeben, dass ich dieses Feld immer auch als ein Objekt einer Tochterklasse instanziieren könnte.
Woran liegt der Fehler?

Grüße aus Karlsruhe
Marco

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mi 20.05.09 13:18 
In deiner HashTable: Dort muss es ? extends Bucket heißen, sonst wird der explizite Typ gefordert. Hatte dieses Problem auch schon mal. Die Meldung von Java ist hier nicht ganz eindeutig.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
baka0815
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 489
Erhaltene Danke: 14

Win 10, Win 8, Debian GNU/Linux
Delphi 10.1 Berlin, Java, C#
BeitragVerfasst: Mi 20.05.09 13:20 
Du möchtest in dem Fall nicht

protected ArrayList<Bucket> bucketList;,

sondern eher

ausblenden C#-Quelltext
1:
protected ArrayList<? extends Bucket> bucketList;					


Du suchst in deinem Fall übrigens nicht nach Polymorphie, sondern nach Generics.

Alternative:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
package hashing;

import java.util.ArrayList;

public abstract class HashTable<E> {
  
  protected ArrayList<E> bucketList;
    
  public int getLength() {
    return this.bucketList.size();
  }

  
}


ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
package hashing;

import java.util.ArrayList;

public class DirectChainedHashTable extends HashTable<DirectChainedBucket> {

  public DirectChainedHashTable() {
    this.bucketList = new ArrayList<DirectChainedBucket>();
    
  }
  
}