Compiler-Fehler Array aus Objekten übergeben

Hallo zusammen,

ich habe eine Klasse Profilpunkt erstellt.
public class Profilpunkt
{

double d;
double h;

/**
* Konstruktor für Objekte der Klasse Profilpunkt
*/
public Profilpunkt(double d, double h)
{
this.d = d;
this.h = h;
}

public double getD()
{
return d;
}

public void setD(double d)
{
this.d = d;
}

public double getH()
{
return h;
}

public void setH(double h)
{
this.h = h;
}

public double steigung(Profilpunkt Q)
{
double dd= Q.d-d;
double dh = Q.h-h;
double udw = dd*dd+dh*dh;
return Math.sqrt(udw);
}

public double hoehendifferenz(Profilpunkt Q)
{
return this.h-Q.h;
}
}

Außerdem eine Klasse Profil, wobei ein Profil aus mehreren Profilpunkten bestehen soll.

public class Profil
{

Profilpunkt[] profilpunkte;
public Profil(Profilpunkt[] profilpunkte)
{
this.profilpunkte = profilpunkte;
}

public Profilpunkt[] getProfilpunkte()
{
return profilpunkte;
}

public void setProfilpunkte(Profilpunkt[] profilpunkte)
{
this.profilpunkte = profilpunkte;
}

}

Wenn ich jetzt zwei Profilpunkte erstelle und diese auf ein neues Profil übergebe kommt:
Profilpunkt cannot be convertes to Profilpunkt[]
Wie kann ich also ein Profil erstellen, das aus mehreren Profilpunkten besteht?
 
Profilpunkt cannot be convertes to Profilpunkt[]
Du musst ein Array übergeben, also entweder so:
Java:
Profilpunkt[] p = new Profilpunkt[2];
p[0] = new Profilpunkt();
p[1] = new Profilpunkt();
new Profil(p);

oder noch einfacher:
Java:
new Profil(new Profilpunkt[]{new Profilpunkt(), new Profilpunkt()});
 
Du kannst so vorgehen:
Java:
        final Profilpunkt[] profilpunkte = new Profilpunkt[2];
        profilpunkte[0] = new Profilpunkt(0.,0.);
        profilpunkte[1] = new Profilpunkt(1.,1.);
        final Profil profil = new Profil(profilpunkte);
 
oder du änderst den Ctor von Profil
Java:
    public Profil(final Profilpunkt... profilpunkte) {
        this.profilpunkte = profilpunkte;
    }
dann kannst du diesen etwas weniger umständlicher verwenden:
Java:
final Profil profil = new Profil(new Profilpunkt(0., 0.), new Profilpunkt(1., 1.));
 
Du musst ein Array übergeben, also entweder so:
Java:
Profilpunkt[] p = new Profilpunkt[2];
p[0] = new Profilpunkt();
p[1] = new Profilpunkt();
new Profil(p);

oder noch einfacher:
Java:
new Profil(new Profilpunkt[]{new Profilpunkt(), new Profilpunkt()});
Hallo, danke! aber wo genau soll ich das jetzt einfügen, sorry bin noch nicht ganz in der Materie drin
 
probiers aus ?

Java:
Profilpunkt...
dieser ausdruck sagt "gib mir eine collection" ... das kann ne liste sein, ein array , oder einfach eine anreihung von objekten falls das unklar ist
 
probiers aus ?

Java:
Profilpunkt...
dieser ausdruck sagt "gib mir eine collection" ... das kann ne liste sein, ein array , oder einfach eine anreihung von objekten falls das unklar ist
Fast ... das ist immer ein Array und nie eine Collection (es sei denn Profilpunkt ist eine Collection).

Java:
  private void testInts() {
    setInts(new Integer[0]); //ok, klassischer Aufruf, falls explizit ein Array verlangt wird anstatt '...'
    setInts(new Integer[]{15, 16}); //ok, klassischer Aufruf, falls explizit ein Array verlangt wird anstatt '...'

    setInts(); //ok, '...' Auruf mit leerem Array, ints.length wird 0 sein.
    setInts(15, 16); //ok, '...' Aufruf Array mit Array Länge 2
    setInts(null); //berechtigte Compilerwarnung, ints wird null sein.

    var a = new ArrayList<Integer>();
    a.add(15);
    a.add(16);
    setInts(a); //nicht kompilierbar, a ist weder Integer noch Integer[]
  }

  private void setInts(Integer... ints) {
      //...
  }
 

Neue Themen


Zurück
Oben