Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to print an array

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to print an array". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to print an array".

"Brother, I heard you say that an array is also an object, but such a class is not clearly defined in Java." It seems that the third sister is studying attentively.

"Yes, so the array doesn't have a chance to override the Object.toString () method. If you try to print the array directly, the output is not what we expected." I went on with what the third sister said.

"then how do you print the array?" The third sister put forward today's core issue with a keen mind.

"first of all, let's take a look at why we can't print the array directly and what will happen if we print it directly."

Let's look at such an example.

String [] cmowers = {"Silence", "Wang er", "an interesting programmer"}; System.out.println (cmowers)

The result printed by the program is:

[Ljava.lang.String;@3d075dc0

[Ljava.lang.String; represents the Class name of the string array, and the @ is followed by hexadecimal hashCode--. The print result is so "human" that most people say they don't understand it! Why is it displayed like this? Just take a look at the toString () method of the java.lang.Object class.

Public String toString () {return getClass () .getName () + "@" + Integer.toHexString (hashCode ());}

Once again, the array is not explicitly defined as a class, but it is indeed an object that inherits all the methods of the ancestor class Object.

"Brother, why doesn't the array define a separate class to represent it, just like the string String class?" The problem of third sister is big, but it is also easy to explain.

"A reasonable argument is that Java hides it. If there is such a class, call it Array.java. Let's imagine what it really looks like. There must be a container to hold every element of the array, just like the String class." While answering the third sister, I opened the source code of the String class.

Public final class String implements java.io.Serializable, Comparable, CharSequence {/ * * The value is used for character storage. * / private final char value [];}

"in the end, you have to store the elements of an array in a form similar to an array, right? this becomes very unnecessary, so you might as well think of the array as an object without form."

"all right, let's not talk about it." I was afraid that the topic would go too far, and I was embarrassed when I couldn't answer it myself. I hastened to drag the train of thought of the third sister back.

"Let's take a look at the first way to print arrays, using a more stylish Stream stream."

The first form:

Arrays.asList (cmowers). Stream (). ForEach (s-> System.out.println (s))

The second form:

Stream.of (cmowers) .forEach (System.out::println)

The third form:

Arrays.stream (cmowers) .forEach (System.out::println)

The printed results are shown below.

Silent Wang er, an interesting programmer.

Yes, all three ways can easily do your job, and seem a bit classy, after all, using Stream, as well as lambda expressions.

"of course, you can also use a more corny way, the for loop. Even for-each."

For (int I = 0; I

< cmowers.length; i++){ System.out.println(cmowers[i]); } for (String s : cmowers) { System.out.println(s); } "哥,你难道忘了上一篇在讲 Arrays 工具类的时候,提到过另外一种方法 Arrays.toString() 吗?"三妹看我一直说不到点子上,有点着急了。 "当然没有了,我认为 Arrays.toString() 是打印数组的最佳方式,没有之一。"我的情绪有点激动。 Arrays.toString() 可以将任意类型的数组转成字符串,包括基本类型数组和引用类型数组。该方法有多种重载形式。 使用 Arrays.toString() 方法来打印数组再优雅不过了,就像,就像,就像蒙娜丽莎的微笑。 (三妹看到这么一副图的时候忍不住地笑了) "三妹,你不要笑,来,怀揣着愉快的心情看一下代码示例。" String [] cmowers = {"沉默","王二","一枚有趣的程序员"}; System.out.println(Arrays.toString(cmowers)); 程序打印结果: [沉默, 王二, 一枚有趣的程序员] 哇,打印格式不要太完美,不多不少!完全是我们预期的结果:[] 表明是一个数组,, 点和空格用来分割元素。 "哥,那如果我想打印二维数组呢?" "可以使用 Arrays.deepToString() 方法。" String[][] deepArray = new String[][] {{"沉默", "王二"}, {"一枚有趣的程序员"}}; System.out.println(Arrays.deepToString(deepArray)); 打印结果如下所示。 [[沉默, 王二], [一枚有趣的程序员]] "说到打印,三妹,哥给你提醒一点。阿里巴巴的 Java 开发手册上有这样一条规约,你看。"

"what is POJO? it is the abbreviation of Plain Ordinary Java Object. When we create a database mapping object in a Web application, we call it POJO. Such objects do not inherit or implement any other Java framework classes or interfaces."

"for such a class, it's best to override its toString () method to make it easier to see what fields the object contains to troubleshoot the problem."

"if not rewritten, the printed Java object is like a printed array, a sequence of characters that no one can understand."

"you can use Intellij IDEA to generate an overridden toString () method, which is particularly convenient."

"all right, brother, I'll keep it in mind. If I encounter you in the future, I'll pay attention to it. You go and have a rest. I'll knock on the code you mentioned and practice it."

"OK, I'll go, I'll go."

Thank you for your reading, the above is the content of "how to print an array", after the study of this article, I believe you have a deeper understanding of how to print an array, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report