next和hasnext_使用Java中的next()和hasNext()方法遍历List元素

next和hasnext

Given a List of integers and we have to traverse, print all its element using next() and hasNext() methods.

给定一个整数列表,我们必须遍历,使用next()和hasNext()方法打印其所有元素。

什么是hasNex()和next()方法? (What are hasNex() and next() methods?)

Both are the library methods of java.util.Scanner class.

两者都是java.util.Scanner类的库方法。

Method hasNext() returns 'true'/'false' - If collection has more values/elements, it returns true otherwise it returns false.

方法hasNext()返回'true'/'false'-如果collection具有更多的值/元素,则返回true,否则返回false 。

Method next() returns the next element in the collection.

方法next()返回集合中的下一个元素。

Note: In this example, we will create/declare an iterator by specifying Integer type - which is base type of the List and then the methods next() and hasNext() will be called through Iterator object.

注意:在此示例中,我们将通过指定Integer类型(这是List的基本类型)来创建/声明一个迭代器,然后将通过Iterator对象调用 next()和hasNext()方法。

Example:

例:

In this example, we will declare a List of integers, add some of the elements and print the elements by using hasNext() and next() methods.

在此示例中,我们将声明一个整数列表,添加一些元素,然后使用hasNext()和next()方法打印这些元素。

Program:

程序:

import java.util.*;
public class ListExample {
public static void main (String[] args) {
//creating a list of integers
List<Integer> int_list = new ArrayList<Integer> ();
int count=0; //variable to count total traversed elements 
//adding some of the elements
int_list.add (10);
int_list.add (20);
int_list.add (30);
int_list.add (40);
int_list.add (50);
//printing elements
System.out.println ("List elements are...");
//creating iterator
Iterator<Integer> it = int_list.iterator ();
while (it.hasNext()){
System.out.println (it.next());
count+=1;
}
System.out.println ("Total traversed elements are: "+count);
}
};

Output

输出量

List elements are...
10
20
30
40
50
Total traversed elements are: 5

翻译自: https://www.includehelp.com/java-programs/traverse-the-list-elements-using-next-and-hasnext-methods-in-java.aspx

next和hasnext

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546155.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

「递归」的正确打开方式,看不懂你打我~

这是磊哥的第 189 期分享作者 | 田小齐来源 | 码农田小齐&#xff08;ID&#xff1a;NYCSDE&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;前言 递归&#xff0c;是一个非常重要的概念&#xff0c;也是面试中非常喜欢考的。因为它不但能考察…

Log4cpp 使用手册

参考资料&#xff1a; log4cpp 配置 与 使用http://www.cnblogs.com/welkinwalker/archive/2011/06/23/2088197.html 便利的开发工具-log4cpp快速使用指南 http://www.ibm.com/developerworks/cn/linux/l-log4cpp/ Log4cpp配置文件格式说明 http://sogo6.iteye.com/blog/115431…

适合初学编程的一些项目

学习了一门语言以及数据结构之后&#xff0c;通常需要做一些项目来巩固所学的知识&#xff0c;我感觉最好是用写一些简单的小工具或者小游戏&#xff0c;能够提高自己的编程能力&#xff0c;也能进一步提高自己学习的兴趣。最好的是将自己想做的事情用程序的实现&#xff0c;比…

python 5的倍数_查找所有低于1000的数字的和,这是Python中3或5的倍数

python 5的倍数Sometimes, we need to find the sum of all integers or numbers that are completely divisible by 3 and 5 up to thousands, since thousands are a too large number that’s why it becomes difficult for us. So, here we will do it in Python programmi…

switch 的性能提升了 3 倍,我只用了这一招!

这是我的第 190 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;上一篇《if快还是switch快&#xff1f;解密switch背后的秘密》我们测试了 if 和 switch 的性能&am…

HashMap get不出对象时出错 解决

为什么80%的码农都做不了架构师&#xff1f;>>> 如题&#xff1a; Map map new HashMap(); map.put("1", "A"); map.put("2", "A"); map.put("3", "A"); map.put("4", "A")…

安装codeblocks和wxwidgets及opencv

codeblocks 是一款开放源代码的跨平台的c/c++集成开发环境,它是用wxwidgets 开发的,并且支持插件,功能很强大。可以用来在windows开发各种程序。下面记录一下我安装该软件的过程: 首先到http://www.codeblocks.org/downloads 下载该软件,选择含mingw的文件(codeblocks-10…

Java LinkedList boolean addAll(int index,Collection c)方法,带有示例

LinkedList boolean addAll(int index&#xff0c;Collection c)方法 (LinkedList boolean addAll(int index, Collection c) method) This method is available in package java.util.Collection and here, Collection is an interface. 该方法在java.util.Collection包中可用…

高质量SQL的30条建议!(后端必备)

这是我的第 191 期分享作者 | 捡田螺的小男孩来源 | 捡田螺的小男孩&#xff08;ID&#xff1a;gh_873ad5979a0b&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;本文将结合实例demo&#xff0c;阐述30条有关于优化SQL的建议&#xff0c;多数…

滤波电容的选择(调试中)

整个系统高电平时12V&#xff0c;信号是ms级的负脉冲&#xff0c;脉冲的下降沿很陡&#xff0c;当用示波器展开看的时候&#xff0c;发现了低于0V的过冲&#xff0c;能低到-6V&#xff08;相当严重&#xff09;&#xff0c;这是在整个箱子的出口处测量到的。在主控板&#xff0…

sicp

sicp (structure and interpretion of computer programs &#xff0c;编号为6.001 )是麻省理工学院的一门经典计算机入门课程&#xff0c;虽然近年被另外一门课程所取代&#xff08;6.00 或6.01 &#xff09;&#xff0c;但最近好像有复活 的迹象。Perter Norvig 写了关于sicp…

Redis的自白:我为什么在单线程的这条路上越走越远?

这是我的第 192 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;我是 Redis&#xff0c;今年 11 岁了~曾几何时我是辣么的单纯&#xff0c;辣么的可爱&#xff0c;而如…

两个矩阵相乘的乘法次数_C ++程序将两个数字相乘而不使用乘法运算符

两个矩阵相乘的乘法次数The problem is we have two integer numbers and find the multiplication of them without using the multiplication operator. This problem can be solved using the Russian peasant algorithm. Assume the two given numbers are m and n. Initia…

关于引用

2019独角兽企业重金招聘Python工程师标准>>> 1、 <!-- lang: html --> <!DOCTYPE HTML> <!-- lang: html --> <html> <!-- lang: html --> <head> <!-- lang: html --> <meta charset"utf-8" /> <!--…

ruby array_在Ruby中使用Array.delete()和Array.delete_at()从Array中移除元素

ruby arrayRuby Array.delete()和Array.delete_at()方法 (Ruby Array.delete() and Array.delete_at() methods) In the last article, we have seen how we can remove the elements from an instance of Array class with the help of Array.pop() and Array.shift()? 在上一…

一口气说出 6 种延时队列的实现方法,面试官满意的笑了

这是我的第 193 期分享作者 | 程序员内点事来源 | 程序员内点事&#xff08;ID&#xff1a;chegnxy-nds&#xff09; 分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;五一期间原计划是写两篇文章&#xff0c;看一本技术类书籍&#xff0c;结果这五天由于自…

一些c语言图形库

想编程绘制一些简单的图形&#xff0c;有不少的图形库可以选择&#xff1a; &#xff08;1&#xff09;BGI图形库&#xff1a;即turbo c所带的图形库。misaki 在vc&#xff08;vc6,vc2008,vc2010等)下重写了该库&#xff0c;名为EGE 。另一个类似的vc(vc6,vc2008,vc2010)下的库…

acl 服务器编程框架特点介绍

2019独角兽企业重金招聘Python工程师标准>>> acl 中服务器框架模块是一个非常重要的模块&#xff0c;使用该模块技术人员可以快速地写出稳定、安全、高效的网络服务应用&#xff0c;该模块主要来源于著名的邮件服务器程序 (Postfix) 中的 master 模块&#xff0c;为…

turbo c相关文档

无意中在网上找到的turbo c 2.0相关文档&#xff0c;有reference guide 和user guide.下载地址见&#xff08;镜像一 &#xff0c;镜像二 &#xff0c;镜像三 &#xff0c;镜像四 &#xff09;。这些网站还有很多其他各类软件相关文档&#xff0c;感兴趣的可以自己看看。另外&a…

一个类可以有一个接口,接口可以有一个Java类吗?

In the very first step, we will see can a class have an interface in Java? 在第一步中&#xff0c;我们将看到类可以在Java中具有接口吗&#xff1f; Yes, it is possible to define an interface inside the class. 是的&#xff0c;可以在类内部定义接口。 The interf…