首页 > IT职业 > IT生活 > 关于 Java 数组的 12 个最佳方法
下面是stackoverflow中关于数组方法的相关问题中,获得最多票数的12个数组操作方法。
Java代码
String[] aArray = new String[5];
String[] bArray = {"a","b","c", "d", "e"};
String[] cArray = new String[]{"a","b","c","d","e"};
Java代码
int[] intArray = { 1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
// print directly will print reference value
System.out.println(intArray);
// [I@7150bd4d
System.out.println(intArrayString);
// [1, 2, 3, 4, 5]
Java代码
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList
System.out.println(arrayList);
// [a, b, c, d, e]
Java代码
String[] stringArray = { "a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
System.out.println(b);
// true
Java代码
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
// Apache Commons Lang library
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
Java代码
method(new String[]{"a", "b", "c", "d", "e"});
Java代码
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] { "a", "b", "c" }, ", ");
System.out.println(j);
// a, b, c
Java代码
String[] stringArray = { "a", "b", "c", "d", "e" };
ArrayList
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
Java代码
Set
System.out.println(set);
//[d, e, b, c, a]
Java代码
int[] intArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
System.out.println(Arrays.toString(intArray));
//[5, 4, 3, 2, 1]
Java代码
int[] intArray = { 1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array
System.out.println(Arrays.toString(removed));
Java代码
byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
for (byte t : bytes) {
System.out.format("0x%x ", t);
}
回到最基础的内容,记得使用工具类,不要盲目,想着偷懒,省时省力
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/912/viewspace-2807502/ ,如需转载,请注明出处,否则将追究法律责任。