繁体   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