ArrayList陣列轉換為二維陣列
阿新 • • 發佈:2018-12-27
因為專案需求,需要將一個List<T>泛型物件轉換成二維陣列,如,一個將一個List<Medicine>轉換為一個二維陣列。
1.首先看測試類吧:
package com.bzjm.test; import java.util.ArrayList; import java.util.List; import com.bzjm.dao.MedicineDAO; import com.bzjm.pojo.Medicine; import com.zml.utils.DataUtils; /** * @author 鄭明亮 * @Time:2016年1月30日 下午8:35:39 * @version 1.0 */ public class TestDao { public static void main(String[] args) { MedicineDAO medicineDAO=new MedicineDAO(); // List<Medicine> list=medicineDAO.querysuitability("123"); List<Medicine> list =getList(); String string[] =list.get(0).toString().split(",");
System.out.println("Medicine物件轉換為字串:"+list.get(0).toString()); System.out.println("二維陣列的第二維的大小:"+string.length); Object [][]o=DataUtils.toArray(list); for(int i = 0;i<list.size();i++ ){ for(int j =i; j<string.length;j++){ System.out.println(o[i][j]); } } } public static List<Medicine> getList(){ List<Medicine> list = new ArrayList<Medicine>(); for (int i = 0; i < 3; i++) { Medicine medicine = new Medicine(); medicine.setMid(i); medicine.setMname("mname:"+i); medicine.setMattention("mattention:"+i); medicine.setMingredient("mingredient:"+i); medicine.setMnoEffect("mnoEffect:"+i); medicine.setMprice("mprice:"+i); medicine.setMsuitability("msuitability:"+i); medicine.setMusage("musage:"+i); list.add(medicine); } return list; } }
可以看到,list.get(0).toString()中資料的欄位是由逗號隔開的,我們則可以通過split(",")將其分割為一個數組,然後獲取陣列的長度,就知道Medicine這個物件一共有幾個成員變量了,拿到這個值,在動態分配二維陣列空間的時候,就可以進行動態分配了
2.List《Medicine》轉換為二維陣列的工具類
package com.zml.utils; import java.util.List; import com.bzjm.pojo.Medicine; import com.google.gson.reflect.TypeToken; /** * @author 鄭明亮 * @Time:2016年2月8日 上午10:53:33 * @version 1.0 */ public class DataUtils { public DataUtils() { // TODO Auto-generated constructor stub } public static <T> Object[][] toArray(List<T> data){
//data.get(0).toString().split(",").length相信大家應該可以理解的,這個是二維陣列第二維的大小,如果不填寫,則會報空指標的錯誤;
//如果填寫具體值 的話,就不能起到動態載入的作用了,這個值不應該是固定的,應該是可變的。
//動態分配一個二維陣列的
Object[][] o=new Object[data.size()][data.get(0).toString().split(",").length];
for (int i = 0; i < data.size(); i++) {
Medicine medicine=(Medicine) data.get(i);
o[i][0]=medicine.getMid();
o[i][1]=medicine.getMname();
o[i][2]=medicine.getMingredient();
o[i][3]=medicine.getMsuitability();
o[i][4]=medicine.getMusage();
o[i][5]=medicine.getMnoEffect();
o[i][6]=medicine.getMattention();
o[i][7]=medicine.getMprice();
}
return o;
}
}
(本想致力於實現一個通用工具類,但是沒想到更好的辦法,如果讀者有更好的辦法可以實現通用List<T>轉換為二維陣列的話,請留言回覆哦,雖然有一種方法可能比較麻煩可以實現,但希望大神能拿出更簡潔更好的方法。)
3.Medicine類
package com.bzjm.pojo;
/**
* Medicine entity. @author MyEclipse Persistence Tools
*/
/**
* @author Adayan
*
*/
/**
* @author 鄭明亮
* @Time:2016年1月30日 下午7:07:43
* @version 1.0
*/
public class Medicine implements java.io.Serializable {
// Fields
private Integer mid;
private String mname;
private String mingredient;//成分
private String msuitability;//適用性
private String musage;//適用,用法
private String mnoEffect;
private String mattention;//注意事項
private String mprice;
// Constructors
/** default constructor */
public Medicine() {
}
/** full constructor */
public Medicine(String mname, String mingredient, String msuitability,
String musage, String mnoEffect, String mattention, String mprice) {
this.mname = mname;
this.mingredient = mingredient;
this.msuitability = msuitability;
this.musage = musage;
this.mnoEffect = mnoEffect;
this.mattention = mattention;
this.mprice = mprice;
}
// Property accessors
public Integer getMid() {
return this.mid;
}
public void setMid(Integer mid) {
this.mid = mid;
}
public String getMname() {
return this.mname;
}
public void setMname(String mname) {
this.mname = mname;
}
public String getMingredient() {
return this.mingredient;
}
public void setMingredient(String mingredient) {
this.mingredient = mingredient;
}
public String getMsuitability() {
return this.msuitability;
}
public void setMsuitability(String msuitability) {
this.msuitability = msuitability;
}
public String getMusage() {
return this.musage;
}
public void setMusage(String musage) {
this.musage = musage;
}
public String getMnoEffect() {
return this.mnoEffect;
}
public void setMnoEffect(String mnoEffect) {
this.mnoEffect = mnoEffect;
}
public String getMattention() {
return this.mattention;
}
public void setMattention(String mattention) {
this.mattention = mattention;
}
public String getMprice() {
return this.mprice;
}
public void setMprice(String mprice) {
this.mprice = mprice;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Medicine [mid=" + mid + ", mname=" + mname + ", mingredient="
+ mingredient + ", msuitability=" + msuitability + ", musage="
+ musage + ", mnoEffect=" + mnoEffect + ", mattention="
+ mattention + ", mprice=" + mprice + "]";
}
}