大家好,又见面了,我是你们的朋友全栈君。
在java中直接输出数组只能得到引用地址
byte[] bytes = {
1, 2, 3, 4, 5};
int[] ints = {
1, 2, 3, 4, 5};
System.out.println(bytes);
System.out.println(ints);
输出
[B@1b6d3586
[S@4554617c
打印数组内容, 得到友好的打印
public static void main(String args[]) throws Exception {
byte[] bytes = {
1, 2, 3, 4, 5};
short[] shorts = {
1, 2, 3, 4, 5};
char[] chars = {
'a', 'b', 'c', 'd', 'e'};
int[] ints = {
1, 2, 3, 4, 5};
long[] longs = {
1, 2, 3, 4, 5};
float[] floats = {
1.1F, 2.2F, 3.3F, 4.4F, 5.5F};
double[] doubles = {
1.1, 2.2, 3.3, 4.4, 5.5};
String[] strings = {
"aa", "bb", "cc", "dd", "ee", "null"};
Integer[] integers = {
1, 2, 3, 4, 5};
int[] ints2 = {};
int[] ints3 = null;
List<String> strings1 = new ArrayList<>();
strings1.add("abc");
strings1.add("def");
strings1.add("xyz");
strings1.add("null");
Map<Integer, String> map = new HashMap();
map.put(1, "aa");
map.put(2, "bb");
map.put(3, "cc");
map.put(4, null);
System.out.println(bytes);
System.out.println(shorts);
System.out.println(chars);
System.out.println(ints);
System.out.println(longs);
System.out.println(floats);
System.out.println(doubles);
System.out.println(strings);
System.out.println(integers);
System.out.println(ints2);
System.out.println(ints3);
System.out.println(strings1);
System.out.println(map);
}
输出:
{
1, 2, 3, 4, 5}
{
1, 2, 3, 4, 5}
{a, b, c, d, e}
{
1, 2, 3, 4, 5}
{
1, 2, 3, 4, 5}
{
1.1, 2.2, 3.3, 4.4, 5.5}
{
1.1, 2.2, 3.3, 4.4, 5.5}
{aa, bb, cc, dd, ee, null}
{
1, 2, 3, 4, 5}
{}
{}
[abc, def, xyz, null]
{
1=aa, 2=bb, 3=cc, 4=null}
public class ArrayUtils {
public static String toString(final Object array) {
if (array == null) {
return "{}";
}
return new ToStringBuilder().append(array).toString();
}
public static class ToStringBuilder {
private final StringBuffer buffer = new StringBuffer(512);
private final ToStringStyle style = new ToStringStyle();
public ToStringBuilder append(final Object obj) {
style.append(buffer, obj);
return this;
}
@Override
public String toString() {
return this.buffer.toString();
}
}
public static class ToStringStyle {
private char arrayStart = '{';
private char arrayEnd = '}';
private String arraySeparator = ", ";
public void append(final StringBuffer buffer, final Object value) {
if (value == null) {
appendNullText(buffer);
} else {
appendInternal(buffer, value);
}
}
private void appendNullText(final StringBuffer buffer) {
buffer.append("");
}
private void appendInternal(final StringBuffer buffer, final Object value) {
register(value);
try {
if (value instanceof Collection<?>) {
appendDetail(buffer, (Collection<?>) value);
} else if (value instanceof Map<?, ?>) {
appendDetail(buffer, (Map<?, ?>) value);
} else if (value instanceof long[]) {
appendDetail(buffer, (long[]) value);
} else if (value instanceof int[]) {
appendDetail(buffer, (int[]) value);
} else if (value instanceof short[]) {
appendDetail(buffer, (short[]) value);
} else if (value instanceof byte[]) {
appendDetail(buffer, (byte[]) value);
} else if (value instanceof char[]) {
appendDetail(buffer, (char[]) value);
} else if (value instanceof double[]) {
appendDetail(buffer, (double[]) value);
} else if (value instanceof float[]) {
appendDetail(buffer, (float[]) value);
} else if (value instanceof boolean[]) {
appendDetail(buffer, (boolean[]) value);
} else if (value.getClass().isArray()) {
appendDetail(buffer, (Object[]) value);
} else {
appendDetail(buffer, value);
}
} finally {
unregister(value);
}
}
private static final ThreadLocal<WeakHashMap<Object, Object>> REGISTRY =
new ThreadLocal<>();
private static Map<Object, Object> getRegistry() {
return REGISTRY.get();
}
private static void register(final Object value) {
if (value != null) {
final Map<Object, Object> m = getRegistry();
if (m == null) {
REGISTRY.set(new WeakHashMap<>());
}
getRegistry().put(value, null);
}
}
static void unregister(final Object value) {
if (value != null) {
final Map<Object, Object> m = getRegistry();
if (m != null) {
m.remove(value);
if (m.isEmpty()) {
REGISTRY.remove();
}
}
}
}
private void appendDetail(final StringBuffer buffer, final boolean[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final byte[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final short[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final char[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final int[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final float[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final double[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final Collection<?> coll) {
buffer.append(coll);
}
private void appendDetail(final StringBuffer buffer, final Map<?, ?> map) {
buffer.append(map);
}
private void appendDetail(final StringBuffer buffer, final long[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
if (i > 0) {
buffer.append(arraySeparator);
}
appendDetail(buffer, array[i]);
}
buffer.append(arrayEnd);
}
private void appendDetail(final StringBuffer buffer, final Object value) {
buffer.append(value);
}
private void appendDetail(final StringBuffer buffer, final Object[] array) {
buffer.append(arrayStart);
for (int i = 0; i < array.length; i++) {
final Object item = array[i];
if (i > 0) {
buffer.append(arraySeparator);
}
if (item == null) {
appendNullText(buffer);
} else {
appendInternal(buffer, item);
}
}
buffer.append(arrayEnd);
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/130863.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...