嵌套LinkedList的.size()

时间:2014-02-14 14:13:14

标签: java linked-list

我有一个嵌套的LinkedList,我需要通过循环运行并需要嵌套列表的.size()。如何访问此嵌套列表?外部列表只有1个元素,该元素具有运行for循环所需的元素数。

LinkedList<LinkedList<Plan>> eligiblePlansList = new LinkedList<>();
LinkedList<String> planNames = new LinkedList<>();
String [] arr;

/**
 * Creates new form ComparePlansGUI
 */
public ComparePlansGUI() {
    initComponents();
}

public ComparePlansGUI(HealthCare hc, Customer c){       
    this.hc = hc;

    eligiblePlansList.add(p.eligiblePlans(c, hc.getPlans()));

    for (int i = 0; i < eligiblePlansList.size(); i++){
        planNames.add(i, eligiblePlansList.get(i).get(i).planName); 
    }

    arr = planNames.toArray(new String[planNames.size()]);

    planNames.clear();

    initComponents();
}

4 个答案:

答案 0 :(得分:0)

试试这个:

eligiblePlansList.get(0).size()

答案 1 :(得分:0)

eligiblePlansList.get(0).size()

答案 2 :(得分:0)

您需要添加额外的循环:

for (int i = 0; i < eligiblePlansList.size(); i++) {
    LinkedList<Plan> planList = eligiblePlansList.get(i); // current list

    for (int j = 0; i < planList.size(); i++) {
        Plan plan = planList.get(j); // current plan
        planNames.add(i, plan.planName); 
    }
}

答案 3 :(得分:0)

for(Linkedlist item:plans)

for(plan p:item)