Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailemVyřešeno Java - Jak získat všechny fields objektu jako String?

OK. Díky. Chápu. Tohle je tedy finální kód

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;

public class Example {

    int first, second;
    String third, fourth;
    static int fifth;

    public Example(int first, int second, String third, String fourth) {
        this.first = first;
        this.second = second;
        this.third = third;
        this.fourth = fourth;
    }

    public ArrayList<String> getAllFields() {
        var fields = new ArrayList<>(Arrays.asList(getClass().getDeclaredFields()));
        var list = new ArrayList<String>(fields.size());
        fields.stream().filter((f) -> !Modifier.isStatic(f.getModifiers())).forEach((f) -> {
            try {
                list.add(f.get(this).toString());
            } catch (IllegalArgumentException | IllegalAccessException ex) {
                System.out.println(ex.getMessage());
            }
        });
        return list;
    }
}

Reakce na odpověď

1 Zadajte svou přezdívku:
2 Napište svou odpověď:
3 Pokud chcete dostat ban, zadejte libovolný text:

Zpět do poradny

loading...