嵌套的 Java 数组列表

huangapple go评论65阅读模式
英文:

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&lt;List&lt;Object&gt;&gt;listoflist=new ArrayList&lt;&gt;();
    List&lt;Object&gt;candidatelist=new ArrayList&lt;Object&gt;();
    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(&quot;what kind of process do you want ? 1-Add Candidate 2-Add Employee &quot; +
                &quot;3- add employee from candidate 4-search candidate for talent 5-print wiew&quot;);
        int processtype=oku.nextInt();

        if (processtype==1){
            System.out.println(&quot;please enter candidate name&quot;);
            String cname=oku.next();
            System.out.println(&quot;please enter candidate sname&quot;);
            String csname=oku.next();
            System.out.println(&quot;please enter candidate adress&quot;);
            String cadress=oku.next();
            System.out.println(&quot;please enter candidate talent&quot;);
            String ctalent=oku.next();
            Candidate.addcandidate(cname,csname,cadress,ctalent);
            System.out.println(&quot;do you want to continue&quot;);
            String answer=oku.next();
            if (answer==&quot;n&quot;){
                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&lt;List&lt;Object&gt;&gt; listoflist = new ArrayList&lt;&gt;();

    public static void main(String[] args) {
        addcandidate(&quot;Name 1&quot;, &quot;Surname 1&quot;, &quot;Street 1&quot;, &quot;Music&quot;);
        addcandidate(&quot;Name 2&quot;, &quot;Surname 2&quot;, &quot;Street 2&quot;, &quot;Singing&quot;);
        addcandidate(&quot;Name 3&quot;, &quot;Surname 3&quot;, &quot;Street 3&quot;, &quot;Sport&quot;);
    }

    public static void addcandidate(String name, String surname, String adress, String talent){
        List&lt;Object&gt; candidatelist = new ArrayList&lt;Object&gt;();
        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&lt;List&lt;Object&gt;&gt;listoflist=new ArrayList&lt;&gt;();

static  void addcandidate(String name,String surname,String adress,String talent){
   
    List&lt;Object&gt;candidatelist = new ArrayList&lt;Object&gt;();    
    
    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&lt;String&gt;candidatelist instead of List&lt;Object&gt;candidatelist for better readability and type checking.

huangapple
  • 本文由 发表于 2020年8月6日 12:41:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63276947.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定