我想使用已提供的2D字符串数据数组创建Owners及其属性的数组列表。我不知道如何转换字符串以使它们成为所有者列表。我需要首先检查所有者列表是否为空,如果是,请将所有者名称添加到列表中。
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"}};
答案 0 :(得分: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,Texas,Arlington,76019,120000,sales,21000,Smith,Residential,61 Bose Lane,Dallas,Texas,75002,310000,65656,Jones,Commercial,2500 West Seventh St,德克萨斯州沃思堡,76015,575000,食品,15750,史密斯,商业,1225 Green Rd,Euless,德克萨斯州,76000,215500,娱乐,9500,琼斯,住宅,5214 Linda Lane,休斯敦,得克萨斯州,77700,195775,98541 ]
答案 1 :(得分:0)
我相信这个功能可以帮到你
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()添加数组