ArrayList的add()方法使用,arraylistadd
ArrayList的add()方法使用,arraylistadd
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 ArrayList
is null
.
You cannot disallow inserting null
to ArrayList
.
避免null
有时可以是有害的,它能把可能的错误。
如果你担心NullPointerException
在某个阶段,你可以查看存储在项目ArrayList
是null
。
你不能不允许插入null
对ArrayList
。
答案 (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
相关文章
- 暂无相关文章
用户点评