我有一个模型,我想制作一个这样的数组。问题是我这样做:
我的代码:
final WifiModel itemsData[] = {
new WifiModel(nearbyAccessPoints.get(0).SSID, "name"),
new WifiModel(nearbyAccessPoints.get(1).SSID, "name"),
new WifiModel(nearbyAccessPoints.get(2).SSID, "name"),
new WifiModel(nearbyAccessPoints.get(3).SSID, "name"),
new WifiModel(nearbyAccessPoints.get(4).SSID, "name"),
};
我想做一个i ++表格,例如i = 4。 我怎么能这样做?
我的模特:
public class WifiModel {
private String SSID;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
WifiModel(String SSID, String name) {
this.SSID = SSID;
this.name = name;
}
public String getSSID() {
return SSID;
}
public void setSSID(String SSID) {
this.SSID = SSID;
}
}
答案 0 :(得分:1)
简单:)
WifiModel itemsData[] = new WifiModel[5];
for(int i=0; i<5; i++) itemsData[i]=new WifiModel(nearbyAccessPoints.get(i).SSID, "name");
答案 1 :(得分:1)
数组不是动态长度。您可以使用ArrayList来实现此目标,如下所示:
ArrayList<WifiModel> itemsData = new ArrayList<>();
int n=4; // any number you want
for(int i=0;i<n;i++)
{
itemData.add(new WifiModel(nearbyAccessPoints.get(i).SSID, "name"));
}
答案 2 :(得分:1)
试试这个:
List<WifiModel> list = new ArrayList<>();
WifiModel wifi=null;
int i;
for(i=0; i<4; i++){
wifi = new WifiModel(nearbyAccessPoints.get(i).SSID, "name");
list.add(wifi);
}