ArrayList相關問題

2022-05-04 06:45:07 字數 4134 閱讀 5407

/**

*使用者類

*/

public

class

user

public user(int

id, string name)

public

intgetid()

public

void setid(int

id)

public

string getname()

public

void

setname(string name)

}

/**

*訂單類

*/public

class

orderform ]

private

string addtime;

private

intuserid;

public

orderform()

public orderform(int id, string goods, string addtime, int

userid)

public

intgetid()

public

void setid(int

id)

public

string getgoods()

public

void

setgoods(string goods)

public

string getaddtime()

public

void

setaddtime(string addtime)

public

intgetuserid()

public

void setuserid(int

userid)

}

/**

*訂單商品類(訂單中包含的商品)

*/public

class

orderformgoods

public orderformgoods(int

id, string good_name, string type)

public

intgetid()

public

void setid(int

id)

public

string getgood_name()

public

void

setgood_name(string good_name)

public

string gettype()

public

void

settype(string type)

}

/**

*評價類 */

public

class

evaluate

public evaluate(int id, string eva_info, int orderformid, int goodid ,int

userid)

public

intgetid()

public

void setid(int

id)

public

string geteva_info()

public

void

seteva_info(string eva_info)

public

intgetorderformid()

public

void setorderformid(int

orderformid)

public

intgetgoodid()

public

void setgoodid(int

goodid)

public

intgetuserid()

public

void setuserid(int

userid)

}

1

public

class

demo ,"

17 + ","

18 + ""

19 + " ]","2015-9-18",1));

20 orderform_list.add(new orderform(2,"["

21 + ","

22 + ""

23 + "]","2015-1-1",1));

24 orderform_list.add(new orderform(3,"["

25 + ""

26 + "]","2011-3-18",1));

27 orderform_list.add(new orderform(4,"["

28 + ","

29 + ","

30 + ","

31 + ","

32 + ""

33 + "]","1998-9-18",2));

3435 listeva_list = new arraylist<>();

36 eva_list.add(new evaluate(1,"不夠結實,斷掉了",1,34,1));

37 eva_list.add(new evaluate(45,"不錯",1,56,1));

38 eva_list.add(new evaluate(33,"不好",1,1,1));

39 eva_list.add(new evaluate(31,"霸氣",2,55,1));

40 eva_list.add(new evaluate(65,"裝逼神器",2,12,1));

41 eva_list.add(new evaluate(56,"技能很帥,秒天秒地",3,32,1));

42 eva_list.add(new evaluate(44,"霸氣",4,80,2));

43 eva_list.add(new evaluate(70,"很不錯的技能",4,79,2));

44 eva_list.add(new evaluate(71,"流星技能很帥",4,78,2));

45 eva_list.add(new evaluate(72,"還不錯",4,99,2));

46//

eva_list.add(new evaluate(73,"防禦力很好",4,565,2));

4748 system.out.println("**********====資料新增完畢!!!**********====");

4950 listofg_list = new arraylist();

51int user_id=1;

5253

for(orderform of : orderform_list) 66}

67}68}

69}7071

return

ofg_list;

7273}74

75public

static

void

main(string args) 80}

81 }

這些示例**是把專案訂單評價這一部分簡單抽象出來的,可以輸出某一使用者評價過的商品,**的主人留下了乙個bug,當某一訂單存在多個商品,這三個商品全部會顯示成最後乙個。

這個bug被指給我,發現造成這個問題的原因是arraylist(50行)的值被覆蓋,**出錯在56行。建立例項的操作寫在for迴圈前面,也就是說每次遍歷(59行)時,都會覆蓋前一次新增到集合裡的值,因為例項物件只有乙個。

ArrayList相關問題

listlist new arraylist 10 list.add s 屬性elementdata,arraylist就是乙個陣列 transient object elementdata public arraylist int initialcapacity else if initialca...

ArrayList相關整理

arraylist是什麼?arraylist是陣列列表,主要用來裝載資料。arraylist的底層實現 arraylist是用陣列來實現的。arraylist的特點 特點 查詢效率 增刪效率低,執行緒不安全。使 頻率很 底層是陣列,那麼我們為什麼可以存放任意數量元素 這個功能,是通過陣列擴容的方式實...

ArrayList相關面試題

arraylist的插入或刪除一定慢嗎?取決於插入或刪除的元素距離有多遠,如果不是最後乙個元素,則在插入或者刪除時,需要移動該位置往後的元素,在插入時且在陣列的末端,如果底層陣列的容量已經小於當前list容量,則根據arraylist的擴容機制需要增大1.5倍的容量,並初始化乙個新的陣列,將原有的資...