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.

Hastables problem...wieder mal ;)

Empfohlene Antworten

Veröffentlicht

 

// Datei Car.java



public class Car

{


	private String name;

	public String typ;

   public short hubraum;

	public String color;

	public int preis;

	public short ps;


	public void setName(String p_name)

	{


		name = p_name;

	}

}

 
 

//	Datei CarCollection.java


import java.util.*;



public class CarCollection

{


	public static void main (String[] args)

	{

		// Bestandsteile der Autos werdern definiert

		Car car01 = new Car();

		car01.setName ("Audi");

		car01.typ = ("A2");

		car01.hubraum = 1269;

		car01.ps = 75;

		car01.color = ("rot");

		car01.preis = 22000;


		Car car02 = new Car();

		car02.setName("Audi");

		car02.typ = ("A3");

		car02.hubraum = 1569;

		car02.ps = 85;

		car02.color = ("weis");

		car02.preis = 29568;


		Car car03 = new Car();

		car03.setName("Audi");

		car03.typ = ("A6");

		car03.hubraum = 1986;

		car03.ps = 101;

		car03.color = ("rot");

		car03.preis = 22000;


		Car car04 = new Car();

		car04.setName("Audi");

		car04.typ = ("A8");

		car04.hubraum = 4520;

		car04.ps = 260;

		car04.color = ("gruen");

		car04.preis = 125253;


		Car car05 = new Car();

		car05.setName("Audi");

		car05.typ = ("S6");

		car05.hubraum = 2998;

		car05.ps = 302;

		car05.color = ("schwarz");

		car05.preis = 22000;


		Car car06 = new Car();

		car06.setName("Audi");

		car06.typ = ("A3");

		car06.hubraum = 1569;

		car06.ps = 123;

		car06.color = ("weis");

		car06.preis = 29568;


		Car car07 = new Car();

		car07.setName("Audi");

		car07.typ = ("A2");

		car07.hubraum = 1269;

		car07.ps = 11;

		car07.color = ("rot");

		car07.preis = 22000;


		Car car08 = new Car();

		car08.setName("Audi");

		car08.typ = ("A3");

		car08.hubraum = 1569;

		car08.ps = 123;

		car08.color = ("weis");

		car08.preis = 29568;


		Car car09 = new Car();

		car09.setName("Audi");

		car09.typ = ("A3");

		car09.hubraum = 1569;

		car09.ps = 24;

		car09.color = ("weis");

		car09.preis = 29568;


		Car car10 = new Car();

		car10.setName("Audi");

		car10.typ = ("A2");

		car10.hubraum = 1269;

		car10.ps = 42;

		car10.color = ("rot");

		car10.preis = 22000;


		Car car11 = new Car();

		car11.setName("Audi");

		car11.typ = ("A3");

		car11.hubraum = 1569;

		car11.ps = 25;

		car11.color = ("weis");

		car11.preis = 29568;


		Car car12 = new Car();

		car12.setName("Audi");

		car12.typ = ("A2");

		car12.hubraum = 1269;

		car12.ps = 22;

		car12.color = ("rot");

		car12.preis = 22000;


		Car car13 = new Car();

		car13.setName("Audi");

		car13.typ = ("A3");

		car13.hubraum = 1569;

		car13.ps = 23;

		car13.color = ("weis");

		car13.preis = 29568;


		//	Autos werden zu der hashtable mit den Schlüsselwort ps hinzugefügt


		Hashtable table = new Hashtable();


		// short wird in ein Object umgewandelt


		table.put(new Short(car01.ps), car01);

		table.put(new Short(car02.ps), car02);

 		table.put(new Short(car03.ps), car03);

		table.put(new Short(car04.ps), car04);

		table.put(new Short(car05.ps), car05);

 		table.put(new Short(car06.ps), car06);

		table.put(new Short(car07.ps), car07);

		table.put(new Short(car08.ps), car08);

 		table.put(new Short(car09.ps), car09);

		table.put(new Short(car10.ps), car10);

		table.put(new Short(car11.ps), car11);

 		table.put(new Short(car12.ps), car12);



		Enumeration e;


		Car currentCar;


		for (e = table.elements() ; e.hasMoreElements() ; )

		{							

			currentCar = (Car)e.nextElement();


			if ( equals(currentCar.ps()) )

			{

			}


			System.out.println( currentCar.ps );

		}

	}

}

 

hi!

ich hab mal einen kleinen code geschrieben....

es will aber nicht funktionieren....

also er soll mir zum schluss zeigen was sich alles in der hashtable befinde....

tut er aber nicht....

weis einer von euch wo mein problem liegt?

thx

gicio

Gibt es den irgendeine Fehlermeldung??

Bei if schleife muss da bei equals nicht noch was stehen!

das ist doch ein Art Vergleich mit Strings oder? und dann braucht es doch 2 Angaben meines Wissens nach!

if ( equals(currentCar.ps()) )

{

}

System.out.println( currentCar.ps );

weis ich nicht.........

und diese fehlermeldung erscheint:

Running: javac CarCollection.java

CarCollection.java:144: cannot resolve symbol

symbol : method ps ()

location: class Car

if ( equals(currentCar.ps()) )

^

1 error

1 error(s)

thx gicio

Stimmt schon. Vor dem equals fehlt noch ein Object.

Die ganze If-Anweisung habe ich mal rausgeschmissen, weil die eigentlich ja nichts macht (auch wenn sie funktionieren würde), weil ja zwischen den beiden {} gar nichts steht.

So bekommst du zumindest alle ps-werte ausgegeben. Wunder dich nicht über die Reihenfolge der Ausgabe. Die ist nämlich mehr oder weniger zufällig: irgendwie hashcode des Wertes dividiert durch anzahl der Elemente und dann wird nach dem Rest der Division sortiert.. (???)


    Enumeration e;

    Car currentCar;

    e = table.elements();

    while (e.hasMoreElements()) {

        currentCar = (Car) e.nextElement();

        System.out.println(currentCar.ps);

    }

Und zudem ist gibt es ja die methode ps() gar nicht??


if ( equals(currentCar.ps()) )

ich hab das mal so gelöst:

 

//	Datei CarCollection.java


import java.util.*;



public class CarCollection

{


	public static void main (String[] args)

	{

		// Bestandsteile der Autos werdern definiert

		Car car01 = new Car();

		car01.setName ("Audi");

		car01.typ = ("A2");

		car01.hubraum = 1269;

		car01.ps = 75;

		car01.farbe = ("rot");

		car01.preis = 22000;


		Car car02 = new Car();

		car02.setName("Audi");

		car02.typ = ("A3");

		car02.hubraum = 1569;

		car02.ps = 85;

		car02.farbe = ("weis");

		car02.preis = 29568;


		Car car03 = new Car();

		car03.setName("Audi");

		car03.typ = ("A6");

		car03.hubraum = 1986;

		car03.ps = 101;

		car03.farbe = ("rot");

		car03.preis = 22000;


		Car car04 = new Car();

		car04.setName("Audi");

		car04.typ = ("A8");

		car04.hubraum = 4520;

		car04.ps = 260;

		car04.farbe = ("gruen");

		car04.preis = 125253;


		Car car05 = new Car();

		car05.setName("Audi");

		car05.typ = ("S6");

		car05.hubraum = 2998;

		car05.ps = 302;

		car05.farbe = ("schwarz");

		car05.preis = 22000;


		Car car06 = new Car();

		car06.setName("Audi");

		car06.typ = ("A3");

		car06.hubraum = 1569;

		car06.ps = 123;

		car06.farbe = ("weis");

		car06.preis = 29568;


		Car car07 = new Car();

		car07.setName("Audi");

		car07.typ = ("A2");

		car07.hubraum = 1269;

		car07.ps = 11;

		car07.farbe = ("rot");

		car07.preis = 22000;


		Car car08 = new Car();

		car08.setName("Audi");

		car08.typ = ("A3");

		car08.hubraum = 1569;

		car08.ps = 123;

		car08.farbe = ("weis");

		car08.preis = 29568;


		Car car09 = new Car();

		car09.setName("Audi");

		car09.typ = ("A3");

		car09.hubraum = 1569;

		car09.ps = 24;

		car09.farbe = ("weis");

		car09.preis = 29568;


		Car car10 = new Car();

		car10.setName("Audi");

		car10.typ = ("A2");

		car10.hubraum = 1269;

		car10.ps = 42;

		car10.farbe = ("rot");

		car10.preis = 22000;


		Car car11 = new Car();

		car11.setName("Audi");

		car11.typ = ("A3");

		car11.hubraum = 1569;

		car11.ps = 25;

		car11.farbe = ("weis");

		car11.preis = 29568;


		Car car12 = new Car();

		car12.setName("Audi");

		car12.typ = ("A2");

		car12.hubraum = 1269;

		car12.ps = 22;

		car12.farbe = ("rot");

		car12.preis = 22000;


		Car car13 = new Car();

		car13.setName("Audi");

		car13.typ = ("A3");

		car13.hubraum = 1569;

		car13.ps = 23;

		car13.farbe = ("weis");

		car13.preis = 29568;


		//	Autos werden zu der hashtable mit den Schlüsselwort ps hinzugefügt


		Hashtable table = new Hashtable();


		// short wird in ein Object umgewandelt


		table.put(new Short(car01.ps), car01);

		table.put(new Short(car02.ps), car02);

 		table.put(new Short(car03.ps), car03);

		table.put(new Short(car04.ps), car04);

		table.put(new Short(car05.ps), car05);

 		table.put(new Short(car06.ps), car06);

		table.put(new Short(car07.ps), car07);

		table.put(new Short(car08.ps), car08);

 		table.put(new Short(car09.ps), car09);

		table.put(new Short(car10.ps), car10);

		table.put(new Short(car11.ps), car11);

 		table.put(new Short(car12.ps), car12);



		Enumeration e;


		Car currentCar;

		System.out.println("Name  Typ Hubraum Ps      Color    Preis");

		for (e = table.elements() ; e.hasMoreElements() ; )

		{							

			currentCar = (Car)e.nextElement();


				System.out.println( currentCar.getName() + "  " +  currentCar.typ + "  " + currentCar.hubraum + "\t  " +  currentCar.ps + "\t  " + currentCar.farbe + "\t   " + currentCar.preis );

		}

	}

}


ist dieser lösungsweg programmiertechnisch   richtig?



thx 


gicio

 [/code]

Prinzipiell sieht das jetzt aus, als würde es funktionieren (habe es nicht ausprobiert).

Allerdings finde ich, dass du die for-Schleife irgendwie mißbrauchst versuch es doch mal mit einer kopfgesteuerten while-Schleife, wie ich es oben in meinem Beitrag gemacht habe, also:

  

while(e.hasMoreElements()){

  // und hier jetzt deine Ausgabe.

}

Vielleicht könntest du deiner Klasse Car auch noch einen zusätzlichen Konstruktor spendieren, mit dem du dann schon beim Instanziieren alle Werte setzen kannst. Dann bräuchtest du nicht für jedes Auto 7 Anweisungen, bis du alle Werte gefüllt hast.

Also als erstes solltest du mal einen Konstruktor für die Klasse Car schreiben, damit du nicht immer jede Variable einzeln definieren mußt (übergeb an den Konstruktor doch einfach die Werte, die du da einzeln angibst, schreib noch einen zweiten Konstruktor, der keine Werte übergeben kriegt [wegen der Flexibilität]). Dann sieht die ganze Sache schon mal viel übersichtlicher aus.

ich hab da mal was geschrieben, vielleicht hilft das:


/*

 * HashTest.java

 *

 * Created on 24. Oktober 2001, 07:45

 */


package test;

import  java.util.*;

/**

 *

 * @author  BigKniffman

 * @version for gicio *fg*

 */

public class HashTest

{

  // HashTable für die Autos

  Hashtable htAutos = null;

  // array für die Autos (noch unbestimmte Länge)

  Car[] car         = null;


  /** Creates new HashTest */

  public HashTest()

  {

    htAutos = new Hashtable();

    start();

    ausgabe();

  }

  /** gibt alle elemente des Hashtables aus */

  private void ausgabe()

  {

    Enumeration eAlle = htAutos.elements();

    Object oKey = null;

    while (eAlle.hasMoreElements())

    {

      oKey = eAlle.nextElement();

      Car currentCar = (Car)oKey;

      System.out.println("Current car: " + currentCar.sName + " Typ: " 

        + currentCar.sTyp + " PS: " + currentCar.iPS); // und so weiter

    }

  }


  private void start()

  {

    // ich hab hier nur mal 3 Autos angelegt (in einem feld (auch array genannt))

    car = new Car [3];

    car[0] = new Car("Audi", "A2", 1269, 75, "rot", 22000);

    car[1] = new Car("Audi", "A3", 1569, 85, "weiss", 29568);

    car[2] = new Car("Audi", "A6", 1986, 101, "rot", 22000);

    // alle elemente des arrays in das hashtable packen

    for (int i=0; i<car.length; i++)

    {

      htAutos.put(new Integer(car[i].iPS), car[i]);

    }

  }


  /**

   * @param args the command line arguments

   */

  public static void main(String args[])

  {

    new HashTest();

  }


  class Car

  {

    public int iHubraum  = -1;

    public int iPS       = -1;

    public int iPreis    = -1;

    public String sName  = null;

    public String sTyp   = null;

    public String sFarbe = null;


    public Car()

    {

    }


    public Car(String sName, String sTyp, int iHubraum, int iPS,

    String sFarbe, int iPreis)

    {

      this.sName    = sName;

      this.sTyp	    = sTyp;

      this.iHubraum = iHubraum;

      this.iPS	    = iPS;

      this.sFarbe   = sFarbe;

      this.iPreis   = iPreis;

    }

  }


}

greetz

Kniff :D:D:D:D

und wäre es nicht angebrachter ein Vector zu benutzen?

gicio

ist dieser ansatz richtig?

  

		Vector car = new Vector();


		Car car01 = new Car();

		v.addElement("Audi");

		v.addElement("A2");

		v.addElement("1269");



thx

gicio

ich hab das mal so gelöst:

 

// Datei CarColectio.java

import java.util.*;


public class CarCollection

{


	public static void main (String[] args)

	{


		// Bestandsteile der Autos werdern definiert

		Car car01 = new Car();

		car01.setName ("Audi");

		car01.typ = ("A2");

		car01.hubraum = 1269;

		car01.ps = 75;

		car01.farbe = ("rot");

		car01.preis = 22000;

		car01.id = 1;


		Car car02 = new Car();

		car02.setName("Audi");

		car02.typ = ("A3");

		car02.hubraum = 1569;

		car02.ps = 85;

		car02.farbe = ("weis");

		car02.preis = 29568;

		car02.id = 2;


		Car car03 = new Car();

		car03.setName("Audi");

		car03.typ = ("A6");

		car03.hubraum = 1986;

		car03.ps = 101;

		car03.farbe = ("rot");

		car03.preis = 22000;

		car03.id = 3;


		Car car04 = new Car();

		car04.setName("Audi");

		car04.typ = ("A8");

		car04.hubraum = 4520;

		car04.ps = 260;

		car04.farbe = ("gruen");

		car04.preis = 125253;

		car04.id = 4;


		Car car05 = new Car();

		car05.setName("Audi");

		car05.typ = ("S6");

		car05.hubraum = 2998;

		car05.ps = 302;

		car05.farbe = ("schwarz");

		car05.preis = 22000;

		car05.id = 5;


		Car car06 = new Car();

		car06.setName("BMW");

		car06.typ = ("320i");

		car06.hubraum = 1569;

		car06.ps = 143;

		car06.farbe = ("weis");

		car06.preis = 41236;

		car06.id = 6;


		Car car07 = new Car();

		car07.setName("VW");

		car07.typ = ("GOLF IV");

		car07.hubraum = 1569;

		car07.ps = 101;

		car07.farbe = ("rot");

		car07.preis = 22365;

		car07.id = 7;


		Car car08 = new Car();

		car08.setName("Audi");

		car08.typ = ("A3");

		car08.hubraum = 1569;

		car08.ps = 123;

		car08.farbe = ("weis");

		car08.preis = 29568;

		car08.id = 8;


		Car car09 = new Car();

		car09.setName("Audi");

		car09.typ = ("A3");

		car09.hubraum = 1569;

		car09.ps = 24;

		car09.farbe = ("weis");

		car09.preis = 29568;

		car09.id = 9;


		Car car10 = new Car();

		car10.setName("Audi");

		car10.typ = ("A2");

		car10.hubraum = 1269;

		car10.ps = 42;

		car10.farbe = ("rot");

		car10.preis = 22000;

		car10.id = 10;


		Car car11 = new Car();

		car11.setName("Audi");

		car11.typ = ("A3");

		car11.hubraum = 1569;

		car11.ps = 25;

		car11.farbe = ("weis");

		car11.preis = 29568;

		car11.id = 11;


		Car car12 = new Car();

		car12.setName("Audi");

		car12.typ = ("A2");

		car12.hubraum = 1269;

		car12.ps = 22;

		car12.farbe = ("rot");

		car12.preis = 22000;

		car12.id = 12;


		Car car13 = new Car();

		car13.setName("Audi");

		car13.typ = ("A3");

		car13.hubraum = 1569;

		car13.ps = 23;

		car13.farbe = ("weis");

		car13.preis = 29568;

		car13.id = 13;


		Vector v = new Vector();


		v.addElement(car01);

		v.addElement(car02);

		v.addElement(car03);

		v.addElement(car04);

		v.addElement(car05);

		v.addElement(car06);

		v.addElement(car07);

		v.addElement(car08);

		v.addElement(car09);

		v.addElement(car10);

		v.addElement(car11);

		v.addElement(car12);

		v.addElement(car13);



		Enumeration e;


		Car currentCar;


		System.out.println("\n\nName  Typ Hubraum Ps      Color    Preis   	 ID\n");


		for (e = v.elements() ; e.hasMoreElements() ; )

		{							

			currentCar = (Car)e.nextElement();


				System.out.println( currentCar.getName() + "  " +  currentCar.typ + "  " + currentCar.hubraum + "\t  " +  currentCar.ps + "\t  " + currentCar.farbe + "\t   " + currentCar.preis + "\t  " + currentCar.id);

		}

	}

}



 

ist diese lösung richtig oder könnte man den letzten teil noch anders machen?

thx gicio

ajo, Vector is hier wirklich besser

folgendermaßen kannst du dir auch den kompletten vector ausgeben lassen:

wobei Vector natürlich n Object vom Typ Vector sein sollte und

du hier nur die Objekte im Vector zurückkriegst


for (int i=0; i<Vector.size(); i++)

{

  System.out.println (Vector.get (i));

}

GENIAL!!!

danke für den coolen tip!!

;)

gicio

:D:P:P:P:D:D

ist es möglich z.b. durch die eingabe einer bestimmten PS-Zahl ein Auto anzeigen zu lassen, das diese PS-Zahl hat?

oder kann man dies nur bei einer hashtable tun?

thx

gicio

:confused: :confused: :rolleyes::rolleyes: :confused: :confused:

In einer Hashtable könntest du wirklich direkt das Auto mit der ps-zahl rausfischen. Dazu müßtest du allerdings ps als Schlüssel benutzen, was an sich wiederrum keinen Sinn macht, weil ein Schlüssel ja immer eindeutig ist. Wenn du also mehrere Autos mit der gleichen PS-Anzahl hast, dann ist am Ende nur das letzte Auto mit dieser PS-Anzahl in der Hashtable. Die anderen überschreibst du wieder.

Also kannst du eigentlich auch bei deinem Vektor bleiben und in die Schleife eine zusätzliche Abfrage einbauen, die ähnlich dem aussieht, was du ganz am Anfang mal gemacht hast:


... 

if ( irgendEinePsZahl == currentCar.ps){

    System.out.println( currentCar.getName() + "  " +  currentCar.typ + "  " + currentCar.hubraum + "\t  " +  currentCar.ps + "\t  " + currentCar.farbe + "\t   " + currentCar.preis + "\t  " + currentCar.id);

}

...

 

aber wie ist es wenn ich z.b. ein menü habe wo man nach 2 sachen suchen kann?

1. suche auto mit hilfe von der ps zahl

2. suche auto mit hilfe der angabe des hubraums

?????

ist sowas überhaupt möglich...beim vektor?

denn bei hashtables kann man ja nur 1 schlüsselwort eingeben soweit ich weis.

thx

gicio

:confused: :confused: :confused:

dann hol dir doch einfach jedes Objekt aus dem Vektor und vergleiche die Attribute des Objekts mit deinen Vorgaben

Bsp:


  int iPS = 20;

  for (int i=0; i<Vector.size(); i++)

  {

    Car currentCar = (Car) Vector.get(i);

    if (currentCar.iPS == iPS)

    {

      System.out.println ("Auto " + currentCar.sName + " gefunden!");

    }

  }

das funzt so, schreib ne methode dafür :P

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

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.