簡體   English   中英

如何將字符串 [][] 轉換為包含字符串 [] 的列表

[英]How do i convert a String [][] to a List containing String []

我的代碼有問題...我收到此錯誤消息:無法在數組類型 String [][] 上調用 toList()

如何將字符串 [][] 轉換為包含字符串 [] 的列表

在 JAVA

    frame.getContentPane().add(comboBoxGetFile);
btnGetInfo.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        String choice = comboBoxGetInfo.getSelectedItem().toString();
        if(choice == "Customers") {
            List<String[]> list = proxy.getAllCustomersInfo().toList();
        }
    }
})

我希望它填滿一張桌子,而不是用 sys.out 打印任何東西

經過一些更改后,我有這個:

frame.getContentPane().add(comboBoxGetFile);
    btnGetInfo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String choice = (String)comboBoxGetInfo.getSelectedItem();
            if(choice.equals("Customers")) {
                try {
                    List<String[]> list = Arrays.asList(proxy.getAllCustomersInfo());
                    //List<String[]> list = Arrays.stream(proxy.getAllCustomersInfo()).collect(Collectors.toList());
                    dtm.setColumnCount(4);
                    dtm.setColumnIdentifiers(colsForShowingCustomers);
                    for (String[] sa : list){
                        dtm.addRow(sa);
                    }

                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    });

如果您使用的是 Java 8,請嘗試以下操作:

List<String[]> list = Arrays.stream(proxy.getAllCustomersInfo()).collect(Collectors.toList());

下面的代碼工作得很好:

package eu.webfarmr;

import java.util.Arrays;
import java.util.List;

public class ListArray {
    public static void main(String[] args) {
        String[][] array = new String[][]{{"hello", "example", "one"},
                                          {"cheerio", "another example", "two"}
                                    };
        List<String[]> list = Arrays.asList(array);

        for (String[] a : list){
            for (String s : a){
                System.out.println(s);
            }
        }
    }
}

它打印出以下內容:

hello
example
one
cheerio
another example
two

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM