ArrayList的add()方法使用


How to avoid null insertion in ArrayList?

如何避免在ArrayList零插入?

Tags:  java  source
标签:  java  source

问题 (Question)

How to avoid null insertion in ArrayList while inserting element?

ArrayList<String> al=new ArrayList<String>();
al.add(null);//should avoid
.
.
.
al.add("Ramesh");

如何避免在ArrayList零插入,插入的元素?

ArrayList<String> al=new ArrayList<String>();
al.add(null);//should avoid
.
.
.
al.add("Ramesh");

最佳答案 (Best Answer)

You could create your own ArrayList-Class (derived from the original) and override the Add-Method
Then you could check for null when Adding.

public boolean Add(E e) {
  if (e == null) return false;
  else return super.Add(e)
}

As Mark stated in the comments you perhaps want to override all other possibilties of Adding values too. (see the doc)

  • add(E e)
  • add(int index, E element)
  • addAll(Collection c)
  • addAll(int index, Collection c)
  • set(int index, E element)

你可以创建你自己的ArrayList类(来自原)和重写Add方法
然后你可以检查是否为null时加入。

public boolean Add(E e) {
  if (e == null) return false;
  else return super.Add(e)
}

在你可能需要重写添加值,所有其他的可能性也评论说,马克。(看到的DOC

  • 添加(E)
  • 添加(int index,元)
  • 所有(集)
  • 所有(int index,集)
  • 集(int index,元)

答案 (Answer) 2

Avoiding null can be harmful sometimes and it could hide possible bugs.

If you're worried about getting NullPointerException in some stage, you can simply check if the item stored in the ArrayListis null.

You cannot disallow inserting null to ArrayList.

避免null有时可以是有害的,它能把可能的错误。

如果你担心NullPointerException在某个阶段,你可以查看存储在项目ArrayListnull

你不能不允许插入nullArrayList

答案 (Answer) 3

You can try something like that, But if you want to do exactly what you are trying you have to rewrite add() in ArrayList class. Using this validation you can avoid null

public static void main(String[] args) {
    ArrayList<String> al=new ArrayList<String>();
    al=add(al,null);
    al=add(al,"Ramesh");
    al=add(al,"hi");
}

public static ArrayList<String> add(ArrayList<String> al,String str){
   if(str!=null){
      al.add(str);
      return al;
   }else {
      return al;
   }
}

In this case you have to call your custom add method to add element

你可以试着这样的事情,但是如果你想做什么,你都要重写add()进入ArrayList类使用此验证可以避免null

public static void main(String[] args) {
    ArrayList<String> al=new ArrayList<String>();
    al=add(al,null);
    al=add(al,"Ramesh");
    al=add(al,"hi");
}

public static ArrayList<String> add(ArrayList<String> al,String str){
   if(str!=null){
      al.add(str);
      return al;
   }else {
      return al;
   }
}

在这种情况下,你必须调用自定义add添加元素的方法

答案 (Answer) 4

ArrayList<String> al = new ArrayList<String>() {

            @Override
            public boolean add(String s ) {

                if( s != null ) {
                    return super.add( s );
                }
                return false;
            }
        };
al.add(null);
al.add("blabla");
ArrayList<String> al = new ArrayList<String>() {

            @Override
            public boolean add(String s ) {

                if( s != null ) {
                    return super.add( s );
                }
                return false;
            }
        };
al.add(null);
al.add("blabla");


//-----------------自己-------------------
在ArrayList中是可以添加null值的,而且size()之后可以看到成功的添加了这个值。究竟为什么可以这样子添加呢?
arrlist.set(0, null);这里将第一个元素置null了。

这里解释了为什么,ArrayList和Array的设计。
http://programmers.stackexchange.com/questions/163489/why-does-java-util-arraylist-allow-to-add-null
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值