Java – die besten Beiträge

Verkapselung von Klasse?

Hallo Coder-Freunde,

meine Freundin hat in einer Übung für die Uni folgende Java-Aufgabe zu lösen:

// ähnlich java.io.File.listFiles(), gibt aber nicht ein Array sondern ein Filez-Objekt zurück, das die File-Objekte des Directory als List<File> verkapselt

public static Filez create(File directory);

Folgendes haben wir bisher gemacht:

class Filez {

public Filez() {

this.list = new ArrayList<File>();

}

public static Filez create(File directory) { // File directory ist ein Objekt mit dem Pfad zum zu durchsuchenden Verzeichnis.

//List<File> list = new ArrayList<File>();

File[] filesAndDirs = directory.listFiles();

// Daten aus Array werden zu Liste hinzugefügt

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

list.add(i, filesAndDirs[i]);

System.out.println(i + ": " + list.get(i));

}

return XXXXX; // Hier hängt's... wie soll eine Liste als Filez-Objekt zurückgegeben werden?

}

}

Mir persönlich erschließt sich die Formulierung "[... gibt ein] Filez-Objekt zurück, das die File-Objekte des Directory als List<File> verkapselt" nicht. Wie soll eine Methode als Returnwert ein Filez-Objekt vom Typ list zurückgeben?

Wir stehen hier auf dem Schlauch, bitte Hilfe!

UPDATE:

Antwort, falls jemand ein ähnliches Problem hat: Konstruktor erstellen!

public Filez() {} oder public Filez(List<File> attrList) { this.list = attrList; }

Return von create() ist dann das gesamte Objekt.

Computer, programmieren, Java, OOP Programmierung

Android Studio: Wieso verändern die Elemente ihre Position?

Hey,

ich habe da ein kleines Problem. Auf dem PC sind alle Elemente dort, wo sie sein sollten, aber wenn ich die App auf dem Handy ausführe, sind die Elemente irgendwie bzw. irgendwo platziert. Kann mir jemand vielleicht sagen, woran es liegen könnte?

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/black_shade_1"
  tools:context=".MainActivity">
  <ImageView 
    android:layout_width="152dp"
    android:layout_height="185dp" 
    app:srcCompat="@drawable/iconqrlogo" 
    android:id="@+id/imageView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="32dp"
    app:layout_constraintHorizontal_bias="0.498" />
  <ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:visibility="visible"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    android:layout_marginTop="56dp"
    app:layout_constraintTop_toBottomOf="@+id/imageView" />
  <Button
    android:text="@string/generateButton"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:id="@+id/generateQrCode"
    app:backgroundTint="@color/yellow"
    android:textColor="#000000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.496"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="160dp" />
  <Button
    android:text="@string/scanCode"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:id="@+id/scanQrCode"
    app:backgroundTint="@color/yellow"
    android:textColor="#000000"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/progressBar"
    android:layout_marginTop="48dp"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintBottom_toTopOf="@+id/generateQrCode"
    app:layout_constraintVertical_bias="0.061" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bild zum Beitrag
Computer, App, programmieren, Java, Android, Android App, Android Studio

Labyrinth Rekursiv durchsuchen Performance?

Ich bin dabei ein Labyrinth Rekursiv zu durchsuchen. Klappt auch bei kleinen Labyrinthen.

Code ist folgender, sollte eigentlich selbsterklärend sein denk ich. Problem ist nun folgendes. Der Professor hat viele JUnit Tests geschrieben und bei großen Labyrinthen dauert es zu lang (4s) und bricht daher ab. Wie schreibe ich den Code Performanter? Oder hab ich einen Denkfehler im Code?

Sprache natürlich Java

Ich kann nicht ausschließen, dass die Tests nicht dazu da sind zu erkennen wie performant der Code generell ist. Heißt, dass man guckt wie viele Tests er schafft um zu wissen welche Performance klasse man so etwa hat.

Stack<Coordinate> shortestPath;

private void recursivelyDiscoveryMaze(int x, int y, int depth, Stack<Coordinate> currentPath) {
    //this.maze.prettyPrint();
    currentPath.push(new Coordinate(x, y));
    if (dX == x && dY == y) {
        if (shortestPath.size() > currentPath.size() || shortestPath.size() == 0) {
            shortestPath = (Stack<Coordinate>) currentPath.clone();
        }
        currentPath.pop();
        return;
    }

    this.maze.getMazeField()[x][y] = 1; //set visited
    depth++;
    if (isValidStep(x + 1, y)) //if can be visited
        recursivelyDiscoveryMaze(x + 1, y, depth, currentPath);
    if (isValidStep(x, y + 1))
        recursivelyDiscoveryMaze(x, y + 1, depth, currentPath);
    if (isValidStep(x - 1, y))
        recursivelyDiscoveryMaze(x - 1, y, depth, currentPath);
    if (isValidStep(x, y - 1))
        recursivelyDiscoveryMaze(x, y - 1, depth, currentPath);
    depth--;
    this.maze.getMazeField()[x][y] = 0; //set unvisited 
    currentPath.pop();
}
Computer, Mathematik, programmieren, Java, Informatik, labyrinth, Rekursion

Meistgelesene Beiträge zum Thema Java