如何删除java中String数组中的所有元素? [英] How to remove all elements in String array in java?

查看:1647
本文介绍了如何删除java中String数组中的所有元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除 String 数组中的所有元素,例如:

I would like to remove all the elements in the String array for instance:

String example[]={"Apple","Orange","Mango","Grape","Cherry"}; 

有任何简单的做法,它上面的任何片段都会有所帮助。谢谢

Is there any simple to do it,any snippet on it will be helpful.Thanks

推荐答案

如果示例 final 然后一个简单的重新分配就可以了:

If example is not final then a simple reassignment would work:

example = new String[example.length];

这假设您需要数组保持相同的大小。如果没有必要,那么创建一个空数组:

This assumes you need the array to remain the same size. If that's not necessary then create an empty array:

example = new String[0];

如果 final 然后你可以 null 输出所有元素:

If it is final then you could null out all the elements:

Arrays.fill( example, null );




  • 参见: void Arrays#fill (对象[],对象)

  • 考虑使用 ArrayList 或类似的集合

  • 这篇关于如何删除java中String数组中的所有元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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