【问题标题】:Inserting String Array Elements into a List将字符串数组元素插入列表
【发布时间】:2018-04-08 06:49:33
【问题描述】:

我想使用提供的二维字符串数据数组创建所有者及其属性的数组列表。我不知道如何转换字符串以使它们成为所有者列表。我需要首先检查所有者列表是否为空,如果是,则将所有者名称添加到列表中。

import java.util.ArrayList;

import javax.swing.JOptionPane;
public class PropertyTest {

public static void main(String[] args) {
    boolean present = true;
    ArrayList <Owner> ownerList = new ArrayList <Owner>();

    String dataArray[][] = {{"Jones","Commercial","123 Mitchell","Arlington","Texas","76019","120000", "sales", "21000"},
                            {"Smith","Residential","61 Bose Lane","Dallas","Texas","75002","310000", "65656"},
                            {"Jones","Commercial","2500 West Seventh St"," Fort Worth","Texas","76015","575000","food", "15750"},
                            {"Smith","Commercial","1225 Green Rd","Euless","Texas","76000","215500","entertainment","9500"},
                            {"Jones","Residential","5214 Linda Lane","Houston","Texas","77700","195775", "98541"}};

【问题讨论】:

  • 你有一个名为Owner的类吗?

标签: java arraylist multidimensional-array polymorphism user-defined-data-types


【解决方案1】:

此代码将帮助您获得正确的输出。 如果我以这个字符串数组为例:java

public <T> List<T> twoDArrayStringToList(T[][] dataArray) {
    List<T> list = new ArrayList<T>();
    for (T[] array : dataArray) {
        list.addAll(Arrays.asList(array));
    }
    return list;
}
public static void main(String args[]){

     String dataArray[][] = {{"Jones","Commercial","123 Mitchell","Arlington","Texas","76019","120000", "sales", "21000"},
             {"Smith","Residential","61 Bose Lane","Dallas","Texas","75002","310000", "65656"},
             {"Jones","Commercial","2500 West Seventh St"," Fort Worth","Texas","76015","575000","food", "15750"},
             {"Smith","Commercial","1225 Green Rd","Euless","Texas","76000","215500","entertainment","9500"},
             {"Jones","Residential","5214 Linda Lane","Houston","Texas","77700","195775", "98541"}};

     System.out.println(twoDArrayStringToList(dataArray));

}

[Jones, Commercial, 123 Mitchell, Arlington, Texas, 76019, 120000, sales, 21000, Smith, Residential, 61 Bose Lane, Dallas, Texas, 75002, 310000, 65656, Jones, Commercial, 2500 West Seventh St, Fort Worth, Texas, 76015, 575000, food, 15750, Smith, Commercial, 1225 Green Rd, Euless, Texas, 76000, 215500, entertainment, 9500, Jones, Residential, 5214 Linda Lane, Houston, Texas, 77700, 195775, 98541 ]

【讨论】:

    【解决方案2】:

    我相信这个功能可以帮到你

     public String[] addString (String[] a , String c){
    
        List<String> list = new ArrayList<String>();
        if (a == null){
    
            list.add(c);
    
            String[] b = new String[list.size()];
    
            for (int j = 0 ; j<list.size() ; j++){
                b[j] = list.get(j);
            }
            return b;
    
        }else {
            for (int i = 0 ; i<a.length ; i++) {
                list.add(a[i]);
            }
            list.add(c);
    
            String[] b = new String[list.size()];
    
            for (int j = 0 ; j<list.size() ; j++){
                b[j] = list.get(j);
            }
    
            return b;
    
        }
    
    
    
    }
    

    使用 for() 添加数组

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      相关资源
      最近更新 更多