Idiot's Note Three: 循环中Break和“<=xxx.length”的细节注意

本文介绍了一个简单的Java程序,用于在整数数组中查找特定数值的位置。通过使用Scanner类获取用户输入,并采用for循环结合break语句来实现高效查找。文章还讨论了数组越界异常的情况。

首先查看代码:

public class BreakTesting  {
	public static void main(String[] args){
		int[] arrayOfInts={32,54,13,69,75,30,11,5,89};
		System.out.println("Type in the Num:");
		Scanner sc=new Scanner(System.in);
		int i=sc.nextInt();
		boolean bl=false;
		int j=0;
		for(j=0;j<arrayOfInts.length;j++){
			if(i==arrayOfInts[j]){
				bl=true;
				break;
			}	
		}
			if(bl){
				System.out.println("Found "+i+" at index "+(j+1));
			}else{
				System.out.println(i+ " not found");
			}
		
	}
}

outprint:

Type in the Num:
5
Found 5 at index 8

  

含break的代码块:

for(j=0;j<arrayOfInts.length;j++){
			if(i==arrayOfInts[j]){
				bl=true;
				break;
			}	
		}

 

break的功能是从该语句所在的switch or 循环中跳出来,执行后续的语句。

 

注意:

arrayOfInts.length是从1开始的,而Index是从0开始的,arrayOfInts[j],如果int j=arrayOfInts.length就会抛出:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
	at Ttt.BreakTesting.main(BreakTesting.java:14)

 

 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.seestech.sell</groupId> <artifactId>medicine</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>medicine</name> <url>http://maven.apache.org</url> <repositories> <repository> <id>nexus-aliyun</id> <name>nexus-aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <layout>default</layout> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <properties> <start-class>com.seestech.sell.App</start-class> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <retrofit2.groupId>com.squareup.retrofit2</retrofit2.groupId> <retrofit2.version>2.5.0</retrofit2.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.9.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.27</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.14</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!--一旦引入了 org.json:json jar 最好移除这个 android-json--> <exclusions> <exclusion> <groupId>com.vaadin.external.google</groupId> <artifactId>android-json</artifactId> </exclusion> <exclusion> <artifactId>asm</artifactId> <groupId>org.ow2.asm</groupId> </exclusion> <exclusion> <artifactId>spring-test</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot JDBC --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--mybatis springboot 启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!--邮件服务--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!--thymeleaf 模版--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>ognl</groupId> <artifactId>ognl</artifactId> <version>3.1.12</version> </dependency> <!--阿里云 OSS--> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.18.2</version> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> <exclusion> <artifactId>java-trace-api</artifactId> <groupId>com.aliyun</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4.14</version> </dependency> <!--tess4j图片识别OCR--> <dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>4.5.5</version> <exclusions> <exclusion> <artifactId>commons-logging</artifactId> <groupId>commons-logging</groupId> </exclusion> <exclusion> <artifactId>commons-io</artifactId> <groupId>commons-io</groupId> </exclusion> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> <exclusion> <artifactId>jna</artifactId> <groupId>net.java.dev.jna</groupId> </exclusion> <exclusion> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> </exclusion> <exclusion> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox-tools</artifactId> </exclusion> </exclusions> </dependency> <!--百度云车牌识别--> <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.16.19</version> <exclusions> <exclusion> <artifactId>guava</artifactId> <groupId>com.google.guava</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.seeta.sdk</groupId> <artifactId>seeta-sdk-platform</artifactId> <version>1.23</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/seeta-sdk-platform-1.23.jar</systemPath> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.arcsoft.face</groupId> <artifactId>json</artifactId> <version>1.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/arcsoft-sdk-face-3.0.0.0.jar</systemPath> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> </exclusions> </dependency> <!-- Spring Kafka Client --> <!--<dependency>--> <!--<groupId>org.springframework.kafka</groupId>--> <!--<artifactId>spring-kafka</artifactId>--> <!--</dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <!-- MYSQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- Alibaba --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.18</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency> <!--poi-ooxml--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- Mybatis_PageHelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.2.1</version> </dependency> <!--fileupload--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.3</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.5</version> </dependency> <!--识别TXT文件编码格式--> <dependency> <groupId>com.googlecode.juniversalchardet</groupId> <artifactId>juniversalchardet</artifactId> <version>1.0.3</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.5.0</version> </dependency> <!--dom4j--> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.1</version> </dependency> <!--aop模块--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--netty--> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.39.Final</version> </dependency> <!--Guava--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.0-jre</version> </dependency> <dependency> <groupId>net.lingala.zip4j</groupId> <artifactId>zip4j</artifactId> <version>2.9.0</version> </dependency> <!--阿里云 短信服务相关jar--> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.1.0</version> </dependency> <!-- 对接 Line --> <dependency> <groupId>${retrofit2.groupId}</groupId> <artifactId>retrofit</artifactId> <version>${retrofit2.version}</version> </dependency> <dependency> <groupId>${retrofit2.groupId}</groupId> <artifactId>converter-gson</artifactId> <version>${retrofit2.version}</version> </dependency> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>logging-interceptor</artifactId> <version>3.12.0</version> </dependency> <!-- JWT --> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream --> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.11</version> </dependency> <!--logback filter 需要--> <!-- https://mvnrepository.com/artifact/org.codehaus.janino/janino --> <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>3.0.15</version> </dependency> <!--hu tool extra--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-extra</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>com.ibm.icu</groupId> <artifactId>icu4j</artifactId> <version>73.2</version> </dependency> <!--转pdf--> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.27</version> </dependency> <!--excel--> <dependency> <groupId>com.idiotalex</groupId> <artifactId>DoExcel</artifactId> <version>1.0.4</version> <exclusions> <exclusion> <artifactId>poi-ooxml</artifactId> <groupId>org.apache.poi</groupId> </exclusion> <exclusion> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </exclusion> </exclusions> </dependency> <!--hutool excel--> <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.12.0</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-poi</artifactId> <version>4.5.6</version> </dependency> <!--easy excel--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.1.2</version> </dependency> <!--Baidu 菜品识别--> <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.15.1</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> </exclusion> <exclusion> <artifactId>guava</artifactId> <groupId>com.google.guava</groupId> </exclusion> </exclusions> </dependency> <!--系统的jar--> <dependency> <groupId>com.idiot</groupId> <artifactId>weichatPay</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/wechatpay-0.0.6.jar</systemPath> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.idiot</groupId> <artifactId>aliPay</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/alipay-sdk-java20170607114101.jar</systemPath> </dependency> <!--中国移动包支付--> <dependency> <groupId>com.idiot</groupId> <artifactId>hiiposm</artifactId> <version>2.0.0.26</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/hiiposm_engine_simple_2.0.0.26.jar</systemPath> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> </exclusions> </dependency> <!--intella sacn2 pay--> <dependency> <groupId>com.idiot</groupId> <artifactId>intella-utility</artifactId> <version>2.1.5</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/intella-utility-2.1.5.jar</systemPath> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> </exclusions> </dependency> <!--处理视频的jar 获取时长--> <dependency> <groupId>com.idiot</groupId> <artifactId>vedio</artifactId> <version>1.0.2</version> <scope>system</scope> <systemPath>${project.basedir}/src/main/resources/lib/jave-1.0.2.jar</systemPath> <exclusions> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.14.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> <version>2.1.6.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.32</version> <!-- 请根据需要使用最新版 --> <scope>provided</scope> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.5.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <!--视频截图依赖--> <dependency> <groupId>com.github.dadiyang</groupId> <artifactId>jave</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-core</artifactId> <version>2.4.4</version> </dependency> <dependency> <groupId>org.fusesource</groupId> <artifactId>sigar</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>org.bytedeco.javacpp-presets</groupId> <artifactId>opencv</artifactId> <version>3.4.3-1.4.3</version> <scope>test</scope> </dependency> <!-- javacv1.5.3 --> <!--<dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>org.bytedeco</groupId> <artifactId>ffmpeg-platform</artifactId> <version>4.2.1-1.5.2</version> </dependency>--> <!--下面的jar表示只在项目运行中生效 用于springboot打war包--> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>--> <!-- 语音识别 --> <dependency> <groupId>net.java.dev.jna</groupId> <artifactId>jna</artifactId> <version>5.7.0</version> </dependency> <dependency> <groupId>com.alphacephei</groupId> <artifactId>vosk</artifactId> <version>0.3.32</version> <exclusions> <exclusion> <artifactId>jna</artifactId> <groupId>net.java.dev.jna</groupId> </exclusion> </exclusions> </dependency> <!-- Linux 环境 --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-linux64</artifactId> <version>2.4.4</version> </dependency> <!-- Windows 环境 --> <dependency> <groupId>ws.schild</groupId> <artifactId>jave-native-win64</artifactId> <version>2.4.4</version> </dependency> </dependencies> <build> <finalName>medicine</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>${start-class}</mainClass> <!--设置启动类--> <jvmArguments>-Dfile.encoding=UTF-8</jvmArguments> </configuration> </plugin> <!-- maven打包的时候告诉maven不需要web.xml,否刚会报找不到web.xml错误 --> <!--<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.4</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> <webResources> <resource> <directory>${project.basedir}/src/main/resources/lib</directory> <includes> <include>*.jar</include> </includes> <targetPath>WEB-INF/lib/</targetPath> </resource> </webResources> </configuration> </plugin>--> </plugins> <!--sonar--> <!--<pluginManagement> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.3.0.603</version> </plugin> </plugins> </pluginManagement>--> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> <resource> <directory>src/main/java/com/seestech/sell/domain</directory> <includes> <include>**/*.xml</include> </includes> <targetPath>com/seestech/sell/domain</targetPath> </resource> <resource> <directory>src/main/resources/lib</directory> <targetPath>BOOT-INF/lib</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources> </build> </project>
10-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值