如何将String [] []转换为包含String []的List

时间:2018-03-06 15:42:42

标签: java

我的代码遇到问题...我收到此错误消息:无法在数组类型String [] []

上调用toList()

如何将String [] []转换为包含String []

的List 在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();
                }
            }
        }
    });

2 个答案:

答案 0 :(得分:0)

如果您使用的是Java 8,请尝试以下操作:

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

答案 1 :(得分:0)

以下代码可以正常使用:

HTML example code 
    <!-- BEGIN: Stock --> 
        <li class="row "> 
            <a href="/quote/quote.html?symb=GOOGL" class="stock"> 
                <span title="Google" class="column stock-name">Google</span> 
                <span stream="last_140864" class="column stock-price">1094.70</span> 
                <span stream="changePct_140864" class="column stock-change"><span class="posData">+0.97%</span></span>

            </a> 
        </li>
        <!-- END: Stock --> 
        <!-- BEGIN: Stock --> 
        <li class="row "> 
            <a href="/quote/quote.html?symb=MSFT" class="stock"> 
                <span title="Microsoft" class="column stock-name">Microsoft</span> 
                <span stream="last_205778" class="column stock-price">93.64</span> 
                <span stream="changePct_205778" class="column stock-change"><span class="posData">+0.63%</span></span>

            </a> 
        </li>
        <!-- END: Stock --> 

打印出以下内容:

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);
            }
        }
    }
}