Необходимо объявить listoflist
вне метода addcandidate
. Например, вот так.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<List<Object>> listoflist = new ArrayList<>();
public static void main(String[] args) {
addcandidate("Name 1", "Surname 1", "Street 1", "Music");
addcandidate("Name 2", "Surname 2", "Street 2", "Singing");
addcandidate("Name 3", "Surname 3", "Street 3", "Sport");
}
public static void addcandidate(String name, String surname, String adress, String talent){
List<Object> candidatelist = new ArrayList<Object>();
candidatelist.add(name);
candidatelist.add(surname);
candidatelist.add(adress);
candidatelist.add(talent);
listoflist.add(candidatelist);
System.out.println(listoflist);
}
}
Результат выполнения этого кода.
[[Name 1, Surname 1, Street 1, Music]]
[[Name 1, Surname 1, Street 1, Music], [Name 2, Surname 2, Street 2, Singing]]
[[Name 1, Surname 1, Street 1, Music], [Name 2, Surname 2, Street 2, Singing], [Name 3, Surname 3, Street 3, Sport]]