一种在Java中初始化空String数组的优雅方法 [英] An elegant way of initialising an empty String array in Java

查看:111
本文介绍了一种在Java中初始化空String数组的优雅方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在处理的当前代码库中,我发现自己需要初始化许多不同长度的空String []。



截至目前,它们是始终按以下方式初始化:

  String [] a = new String [] {,,, ,,,,,} //等... 

虽然这是一个简单的单行解决方案,但我个人的偏好是相当难看。



我只是想知道是否有人知道任何现有的API /实用程序这提供了一种方法,可以更优雅地初始化空字符串数组。我正在考虑以下几点:

  StringUtils。* initialiseEmptyArray *(int size); 

有谁知道这种方法吗?



<如果需要的话,我总是可以写自己的,只是不想重新发明它已经完成的轮子。



谢谢

解决方案

您可以使用 Arrays.fill 方法: -

  Arrays.fill(a,); 


In the current code base that I'm working on I find myself needing to initialise many empty String[] of different lengths.

As of now, they are always initialised in the following way:

String[] a = new String[]{"","","","","","","","",""} // etc...

Although this is a simple one line solution, my personal preference is that it's rather ugly.

I was just wondering if anyone is aware of any existing API/Utility that offers a method where an array of empty strings can be initialised more elegantly. I was thinking something along the lines of:

StringUtils.*initialiseEmptyArray*(int size);

Does anyone know of any such method?

I can always write my own if need be, just didn't want to re-invent the wheel if its already been done.

Thanks

解决方案

You can use Arrays.fill method: -

Arrays.fill(a, "");

这篇关于一种在Java中初始化空String数组的优雅方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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