Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Hallo Community,

ich hab ein Problem mit den Generics.

Und zwar bin ich dabei den ShellSort zu implementieren.

Der ShellSort ist eine statische Methode die ein Generic erhält, dass mir jetzt Probleme bereitet. Den wenn ich den Shellsort aufrufen möchte, krieg ich irgendwie CastProbleme.

Hier mal der Code vom Shellsort


	static <K extends Comparable<K>>void shell_sort(K[] field, Shell sh)

	{

		int iDist = sh.IDiff(field.length); // Der Code ist natürlich noch nicht fertig

                System.out.println(iDist);


	}

Die Methode wird in meiner Klasse ArrayToSort aufgerufen, die am Ende dem ShellSort ein Interarray übergibt. Auch den Code will ich nicht verheimlichen

public class ArrayToSort extends Frame

{

	PixelGrabber grab;

	final int W = 500;

	final int H = 330;

	int [] m_Pix = new int [W*H];

	Image pic;


	public ArrayToSort() 

	{

		setSize(W,H);

		pic = getToolkit().createImage("Katze.jpg");

		PixelGrabber grab = new PixelGrabber(pic, 0, 0, W, H, m_Pix, 0, W);

		try {

			grab.grabPixels();

		} catch (InterruptedException e) {}

	}

	public void paint (Graphics g)

	{

		g.drawImage(pic,0,0,this);

	}

	public int[] getArray() // Hier wird das Array bereitgestellt

	{

		return m_Pix;

	}

	public static void main (String args [])

	{

		ArrayToSort Array = new ArrayToSort();

		Shell sh = new Shell();

		Shell_Sort.shell_sort(Array.getArray(), sh); 

// Meine statische Methode befindet sich in der Klasse Shell_Sort. Deswegen am Anfang

// nochmal der Aufruf Shell_Sort

	}

}

Folgender Fehler wird ausgespuckt: Das das int [] nicht vom Datentyp K ist. Das versteh ich ja auch, aber wie kann ich dieses Problem lösen. Ich meine ich muss doch eig. in meiner statischen Methode jeglichen Datentyp übergeben können, der sortiert werden könnte. Fehlen mir ein paar Genericseinträge?

Hoffe ich konnte mich vernünftig erklären

Problem gelöst.

Häts du mir nicht einfach sagen können, dass mein int Array kein Comparable besitzt und das man statt int ein Integer Array übergeben muss? ;-)

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.