英文:
Nested Array list in Java
问题
以下是已翻译的部分:
I am asking user candidate info then i am calling 'addcandidate' method and i am trying to hold candidate info in array list. By the way if user want to add new candidate i am getting again their info and want to add my arraylist. But its holding only last candidate info.
my addcandidate method code like this
static void addcandidate(String name, String surname, String adress, String talent) {
List<List<Object>> listoflist = new ArrayList<>();
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);
}
and also my code that to ask candidate info like that
while (true) {
Scanner oku = new Scanner(System.in);
System.out.println("what kind of process do you want ? 1-Add Candidate 2-Add Employee " +
"3- add employee from candidate 4-search candidate for talent 5-print wiew");
int processtype = oku.nextInt();
if (processtype == 1) {
System.out.println("please enter candidate name");
String cname = oku.next();
System.out.println("please enter candidate surname");
String csname = oku.next();
System.out.println("please enter candidate address");
String cadress = oku.next();
System.out.println("please enter candidate talent");
String ctalent = oku.next();
Candidate.addcandidate(cname, csname, cadress, ctalent);
System.out.println("do you want to continue");
String answer = oku.next();
if (answer.equals("n")) {
break;
}
}
}
my output expectation is like this [[tom, jhn, usa, it], [peter, parker, england, language]]
英文:
I am asking user candidate info then i am calling 'addcandidate' method and i am trying to hold candidate info in array list.By the way if user want to add new candidate i am getting again their info and want to add my arraylist.But its holding only last candidate info
my addcandidate method code like this
static void addcandidate(String name,String surname,String adress,String talent){
List<List<Object>>listoflist=new ArrayList<>();
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);
}
and also mycode that to ask candidate info like that
while (true){
Scanner oku=new Scanner(System.in);
System.out.println("what kind of process do you want ? 1-Add Candidate 2-Add Employee " +
"3- add employee from candidate 4-search candidate for talent 5-print wiew");
int processtype=oku.nextInt();
if (processtype==1){
System.out.println("please enter candidate name");
String cname=oku.next();
System.out.println("please enter candidate sname");
String csname=oku.next();
System.out.println("please enter candidate adress");
String cadress=oku.next();
System.out.println("please enter candidate talent");
String ctalent=oku.next();
Candidate.addcandidate(cname,csname,cadress,ctalent);
System.out.println("do you want to continue");
String answer=oku.next();
if (answer=="n"){
break;
}
}
}
my output expectation is like this [[tom,jhn,usa,it],[peter,parker,england,language]]
答案1
得分: 2
你需要在addcandidate
方法外声明listoflist
。例如像这样。
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]]
英文:
You need declare listoflist
out of method addcandidate
. For example like this.
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);
}
}
The result of executing this code.
[[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]]
答案2
得分: 1
Your listoflist
declaration is inside the addCandidate
method, so each time the ArrayList is being created, and finally, you have the list that was created last. Do something like below:
// outside the addCandidate method
List<List<Object>> listoflist = new ArrayList<>();
static void addCandidate(String name, String surname, String address, String talent) {
List<Object> candidateList = new ArrayList<>();
candidateList.add(name);
candidateList.add(surname);
candidateList.add(address);
candidateList.add(talent);
listoflist.add(candidateList);
System.out.println(listoflist);
}
Hope you got the point.
Additionally, use List<String> candidateList
instead of List<Object> candidateList
for better readability and type checking.
英文:
Your listoflist
declaration is inside addCandiadte
method so each time the array list is being created and finally you are having the list that was created last. Do something like below:
// outside addCandiadte method
List<List<Object>>listoflist=new ArrayList<>();
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);
}
Hope you got the point.
Additionally use List<String>candidatelist
instead of List<Object>candidatelist
for better readability and type checking.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论