In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use Java array iteration, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
1. Overview
First of all, we iterate over the array by traversing the values of the array. Secondly, we use the older method of Java, and finally we use the Stream API of Java 8 to convert the array into an iterator.
In addition, we can apply this technique to string processing.
2. Iterate the array through a loop
Iterative logic is established on the array through a loop, and the corresponding values are obtained from the array according to the index.
Code:
Package cn.java4u.codebase.array.iterator;/** * @ author Snail * @ from official account: snail Internet * / public class ArrayIterate {public static void main (String [] args) {/ / string array String [] names = new String [] {"john", "Amal", "Paul"}; / / iterating array over its values. For (int index=0; index
< names.length ; index++) { System.out.println(names[index]); } }} 输出: john Amal Paul 3、JDK 8 之前使用老方式进行数组迭代 JDK 8 之前,在数组转列表的时候,按照老方式我们通常使用 Arrays.asList() 方法先得到一个 list,然后使用 list.iterator() 方法得到一个迭代器,最后遍历迭代器里所有的值。 package cn.java4u.codebase.array.iterator;import java.util.Arrays;import java.util.Iterator;import java.util.List;/** * @author 蜗牛 * @from 公众号:蜗牛互联网 */public class JavaArrayToIterableExample { public static void main(String[] args) { // string array String[] names = new String[]{"john", "Amal", "Paul"}; // string array to list conversion List namesList = Arrays.asList(names); // List to iterable Iterator it = namesList.iterator(); // printing each value from iterator. while (it.hasNext()) { System.out.println(it.next()); } }} 输出: john Amal Paul 4、使用 Java 8 Stream 进行数组迭代 上一小节中,我们是通过 Arrays.asList() 把数组转成列表,现在我们使用 Java 8 Stream API 也可以做到,只需要用 Arrays.stream() 替换就行,它接收数组并返回数组类型的数据流。 stream() 方法可以把数组转换为 Stream 对象,在较大的数组上使用并行执行能力,然后需要调用 Stream 对象上的 iterator() 方法将 Stream 转换为 Iterator。 参考代码如下: package cn.java4u.codebase.array.iterator;import java.util.Arrays;import java.util.Iterator;import java.util.stream.Stream;/** * @author 蜗牛 * @from 公众号:蜗牛互联网 */public class JavaArrayToIterableExampleJava8 { public static void main(String[] args) { // string array String[] names = new String[] {"john", "Amal", "Paul"}; System.out.println("多行打印的解决方案"); // Convert string array to Stream Stream namesList = Arrays.stream(names); // Stream to iterable Iterator it = namesList.iterator(); // printing each value from iterator. while(it.hasNext()) { System.out.println(it.next()); } // singel line System.out.println("\n单行打印的解决方案"); Arrays.stream(names).iterator().forEachRemaining(name ->System.out.println (name);}}
Multi-line and single-line solutions provide the same output. If you're going to use it in a real project, it's best to use an one-line statement, because it looks more like an expert and takes advantage of Stream!
The solution of multi-line printing
John
Amal
Paul
The solution of single-line printing
John
Amal
Paul
5. The application of string
If you understand the above, it's easy to apply iteration capabilities to strings.
For example, we have a string that separates different contents with spaces, assuming that: 1, 2, 3, 4, 5, 6, we need to print each content separately, and we can apply Java 8 Stream for iterative processing.
Package cn.java4u.codebase.array.iterator;import java.util.Arrays;import java.util.Iterator;import java.util.stream.Stream;/** * @ author Snail * @ from official account: snail Internet * / public class JavaStringToIterableCase {public static void main (String [] args) {/ / string String numbers = "1 2 3 4 56"; / / string to string array String [] numbersArray = numbers.split ("") System.out.println ("solution for multiline printing"); / / Convert string array to Stream Stream numbersList = Arrays.stream (numbersArray); / / Stream to iterable Iterator it = numbersList.iterator (); / / printing each value from iterator. While (it.hasNext ()) {System.out.println (it.next ());} / / singel line System.out.println ("\ nSolutions for single-line printing"); Arrays.stream (numbersArray) .iterator () .forEachRemaining (name-> System.out.println (name));}}
Output:
The solution of multi-line printing
one
two
three
four
five
six
The solution of single-line printing
one
two
three
four
five
six
Thank you for reading this article carefully. I hope the article "how to use Java Array iterations" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.