「Java」リスト(List)へのクラスオブジェクト(class)の要素を追加する方法
構文
List<クラス名> cftLst = new ArrayList<>();
EmpProfileクラスの定義
package com.arkgame.study;
public class EmpProfile {
// 会社
String company;
// 規模
String comType;
// 利用フラグ
Boolean useFlg;
public EmpProfile(String company, String comType, Boolean creFlg) {
super();
this.company = company;
this.comType = comType;
this.useFlg = creFlg;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getComType() {
return comType;
}
public void setComType(String comType) {
this.comType = comType;
}
public Boolean getUseFlg() {
return useFlg;
}
public void setUseFlg(Boolean useFlg) {
this.useFlg = useFlg;
}
}
package com.arkgame.study;
public class EmpProfile {
// 会社
String company;
// 規模
String comType;
// 利用フラグ
Boolean useFlg;
public EmpProfile(String company, String comType, Boolean creFlg) {
super();
this.company = company;
this.comType = comType;
this.useFlg = creFlg;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getComType() {
return comType;
}
public void setComType(String comType) {
this.comType = comType;
}
public Boolean getUseFlg() {
return useFlg;
}
public void setUseFlg(Boolean useFlg) {
this.useFlg = useFlg;
}
}
package com.arkgame.study; public class EmpProfile { // 会社 String company; // 規模 String comType; // 利用フラグ Boolean useFlg; public EmpProfile(String company, String comType, Boolean creFlg) { super(); this.company = company; this.comType = comType; this.useFlg = creFlg; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getComType() { return comType; } public void setComType(String comType) { this.comType = comType; } public Boolean getUseFlg() { return useFlg; } public void setUseFlg(Boolean useFlg) { this.useFlg = useFlg; } }
リストへEmpProfileクラスを追加
package com.arkgame.study;
import java.util.ArrayList;
import java.util.List;
public class ListMapObjectDemo {
public static void main(String[] args) {
List<EmpProfile> cftLst = new ArrayList<>();
//リストへクラスオブジェクト要素の追加
cftLst.add(new EmpProfile("companyA_01", "type1", true));
cftLst.add(new EmpProfile("companyB_02", "type2", false));
cftLst.add(new EmpProfile("companyC_03", "type3", true));
for (EmpProfile cft : cftLst) {
System.out.println("company: " + cft.getCompany() +
"comtype: " + cft.getComType() +
"userFlg: " + cft.getUseFlg());
}
}
}
package com.arkgame.study;
import java.util.ArrayList;
import java.util.List;
public class ListMapObjectDemo {
public static void main(String[] args) {
List<EmpProfile> cftLst = new ArrayList<>();
//リストへクラスオブジェクト要素の追加
cftLst.add(new EmpProfile("companyA_01", "type1", true));
cftLst.add(new EmpProfile("companyB_02", "type2", false));
cftLst.add(new EmpProfile("companyC_03", "type3", true));
for (EmpProfile cft : cftLst) {
System.out.println("company: " + cft.getCompany() +
"comtype: " + cft.getComType() +
"userFlg: " + cft.getUseFlg());
}
}
}
package com.arkgame.study; import java.util.ArrayList; import java.util.List; public class ListMapObjectDemo { public static void main(String[] args) { List<EmpProfile> cftLst = new ArrayList<>(); //リストへクラスオブジェクト要素の追加 cftLst.add(new EmpProfile("companyA_01", "type1", true)); cftLst.add(new EmpProfile("companyB_02", "type2", false)); cftLst.add(new EmpProfile("companyC_03", "type3", true)); for (EmpProfile cft : cftLst) { System.out.println("company: " + cft.getCompany() + "comtype: " + cft.getComType() + "userFlg: " + cft.getUseFlg()); } } }