亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這里有一個關于java的問題,在list.remove方法使用?

這里有一個關于java的問題,在list.remove方法使用?

慕容森 2021-05-19 14:11:17
public List updateProduct(List lists,String[] productId1)throws Exception{Connection conn=null;PreparedStatement prep=null;ResultSet rs=null;List all=new ArrayList();for (int i=0;i<productId1.length;i++){lists.remove(new Integer(productId1[i]));}System.out.println();try{for(int i=0;i<lists.size();i++){Product product1=(Product)lists.get(i);int productId=product1.getProductId();System.out.println("剩下的商品Id="+productId);String sql="select * from product where productId ="+productId; conn= new DBConnection().getConnection();//System.out.println("sql11111111111111="+sql);prep=conn.prepareStatement(sql);rs = prep.executeQuery();while (rs.next()){Product product=new Product();product.setProductId(rs.getInt("productId"));product.setProductCode(rs.getString("productCode"));product.setProductName(rs.getString("productName"));product.setUnit(rs.getString("unit"));product.setPremark(rs.getString("premark"));all.add(product);}}}finally{if(prep!=null)prep.close();if(prep!=null)prep.close();if(conn!=null)conn.close();} return all; }比如lists里面有51,52,58,59四個數,如何刪除productId1中51,52這兩個數可是如果我不知道要刪除元素的位置該怎么辦呢 不能根據public E remove(int index) 來刪除,而且我想刪的是數組中的一個或多個,product【i】也不是int型 也不能用lists.remove(new Integer(productId1[i])); 刪除; 該怎么刪除 string 型 productId1【i】值 雖然知道productId【i】的值是int型 我修改了下 for (int i=0;i<productId1.length;i++){lists.remove(new Integer(new Integer(productId1[i]).intValue()));System.out.println("測試輸出="+new Integer(productId1[i]).intValue());}輸出結果 測試輸出=58測試輸出=59剩下的商品Id=58連接數據庫成功剩下的商品Id=59連接數據庫成功剩下的商品Id=60連接數據庫成功剩下的商品Id=61連接數據庫成功可惜還是沒刪掉 如果剩下商品Id只有60,61就對了
查看完整描述

3 回答

?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

ArrayList下的remove方法:

public boolean remove(Object o)

移除此列表中首次出現的指定元素(如果存在)。如果列表不包含此元素,則列表不做改動。更確切地講,移除滿足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引的元素(如果存在此類元素)。如果列表中包含指定的元素,則返回 true(或者等同于這種情況:如果列表由于調用而發生更改,則返回 true)。

因為int是object的子類,所以可以傳入int類型參數來刪除.
-----------------------------------------------------------

public E remove(int index)

移除此列表中指定位置上的元素。向左移動所有后續元素(將其索引減 1)。

也可以循環找出要刪除的數的下標然后這個方法刪除

查看完整回答
反對 回復 2021-05-23
?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

因為list是有順序的,先add的編號就?。◤?開始),這樣就可以通過remove(編號)的形式進行刪除,之后后面的會編號依次變?。ㄒ簿褪钦f編號總是連續的)。舉例:
List list = new linkedList();
list.add("0");
list.add("1");
list.remove(0);
結果就是:list.get(0) =1;
備注:如果再一次“list.remove(0);”那么list對象就是個空。

查看完整回答
反對 回復 2021-05-23
  • 3 回答
  • 0 關注
  • 265 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號