如何从Java中链表中删除数据 [英] how to remove data from linkedlist in java

查看:296
本文介绍了如何从Java中链表中删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建android系统演示的购物车这个我使用Application类保存数据。我无法从链表中删除数据。我呼吁为Android活动的removeItem()函数从列表中删除所选的项目,但它不工作的任何人能帮助我。

 包in.co.santoshsharma.bookshopping;进口java.util.LinkedList中;进口android.app.Application;
进口android.content.res.Configuration;公共类全球国际扩展应用{
    私人字符串电子邮件;
    私人字符串ITEMNAME;
    私人诠释itemQuantity;
    私人诠释itemCost;    公共全球国际会议(){    }
    公共全球国际(字符串INAME,诠释iQunt,诠释iCost){
        // TODO自动生成构造函数存根
        this.itemCost = iCost;
        this.itemName = INAME;
        this.itemQuantity = iQunt;    }
    公共无效setEmail(字符串的邮件)
    {
        this.email =邮件;
    }    公共字符串getEmail()
    {
        返回的电子邮件;
    }
    公共字符串getItemName()
    {
        返回ITEMNAME;
    }    公众诠释getItemCost()
    {
        返回itemCost;
    }    公众诠释getItemQunt()
    {
        返回itemQuantity;
    }    LinkedList的<全球国际>名单=新的LinkedList<全球国际>();    公共无效SETLIST(字符串INAME,诠释iQunt,诠释iCost)
    {
        list.add(新全球国际(INAME,iQunt,iCost));
    }    公众的LinkedList<全球国际>的GetList()
    {
        返回列表;
    }
    公共无效的removeItem(字符串INAME,诠释iQunt,诠释iCost)
    {
        对于(全球国际数据:名单)
        {
            如果(data.getItemName()。等于(INAME))
            {
                list.remove(ITEMNAME);
                //list.remove(iCost);
                //list.remove(iQunt);
            }
        }
    }}


解决方案

首先,覆盖等于()方法,并使用 ITEMNAME 进行比较的属性。

  @覆盖
公共布尔等于(对象o){
  如果(O == NULL)返回false;
  如果(ITEMNAME == NULL)返回false;
  如果(O}这种字符串)返回itemName.equals(O);
  否则,如果(O}这种全球国际)回报((全球国际)O).itemName.equals(this.itemName);
  否则返回false;
}

然后,改变你的的removeItem()

 公共无效的removeItem(字符串INAME){
  list.remove(INAME);
  //或取消注释线下完全删除所有匹配的元素
  //为(;; list.remove(INAME)){}
}

据<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html#remove%28java.lang.Object%29\" rel=\"nofollow\">http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html#remove(java.lang.Object) 删除())一个的LinkedList 将调用等于办法(提供的对象的方法,并将其与列表中的每一个元素进行比较。

希望这有助于:)

I am creating a demo shopping cart in android for this i am using Application class for saving data. I am unable to delete data from linkedlist. I am calling removeItem() function for android activity for removing selected item from the list but it is not working any one can help me.

    package in.co.santoshsharma.bookshopping;

import java.util.LinkedList;

import android.app.Application;
import android.content.res.Configuration;

public class GlobalData extends Application{
    private String email;
    private String itemName;
    private int itemQuantity;
    private int itemCost;

    public GlobalData(){

    }
    public  GlobalData(String iName,int iQunt,int iCost) {
        // TODO Auto-generated constructor stub
        this.itemCost=iCost;
        this.itemName=iName;
        this.itemQuantity=iQunt;

    }




    public void setEmail(String mail)
    {
        this.email=mail;
    }

    public String getEmail()
    {
        return email;
    }
    public String getItemName()
    {
        return itemName;
    }

    public int getItemCost()
    {
        return itemCost;
    }

    public int getItemQunt()
    {
        return itemQuantity;
    }



    LinkedList<GlobalData> list = new LinkedList<GlobalData>();

    public void setList(String iName,int iQunt,int iCost)
    {
        list.add(new GlobalData( iName, iQunt, iCost));
    }

    public LinkedList<GlobalData> getList()
    {
        return list;
    }
    public void removeItem(String iName,int iQunt,int iCost)
    {
        for(GlobalData data:list)
        {
            if(data.getItemName().equals(iName))
            {
                list.remove(itemName);
                //list.remove(iCost);
                //list.remove(iQunt);
            }
        }
    } 



}

解决方案

First, override equals() method and use itemName attribute for the comparison

@Override
public boolean equals(Object o) {
  if (o == null) return false;
  if (itemName == null) return false;
  if (o instanceOf String) return itemName.equals(o);
  else if (o instanceOf GlobalData) return ((GlobalData) o).itemName.equals(this.itemName);
  else return false;
}

Then, change your removeItem() method

public void removeItem(String iName) {
  list.remove(iName);
  // or uncomment line below to completely remove all matching elements
  // for (;;list.remove(iName)) {}
}

According to http://docs.oracle.com/javase/6/docs/api/java/util/LinkedList.html#remove(java.lang.Object) remove() method of a LinkedList will call the equals() method of the supplied Object and compare it with every element in the list.

Hope this helps :)

这篇关于如何从Java中链表中删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆