循环与一维数组

102 阅读2分钟

这是我参与2022首次更文挑战的第6天,活动详情查看:2022首次更文挑战 | 创作学习持续成长,夺宝闯关赢大奖 - 掘金 (juejin.cn)

(六)循环结构

循环结构在某些条件满足的情况下,反复执行特定代码的功能

循环语句分类

  • for 循环
  • while 循环
  • do-while 循环

1.for循环

/*
For循环的使用
一、循环结构的四个要素
1.初始化条件
2.循环条件  (boolean)
3.循环体
4.迭代条件
二、for循环的条件
​
for(1;2;4){
    3;
}
​
执行过程:1-->2-->3-->4-->2-->3-->4-->2......-->2
*/
​
class ForTest{
    public static void main(String[] args){
​
        for (int i = 1;i <= 5;i++) {
            System.out.println("HelloWorld");
        }
​
        //练习:
        int num = 1;
        for(System.out.println('1');num <= 10;System.out.println('3'),num++){
            System.out.println('2');
        }
​
        //遍历100内的偶数,输出所有偶数的和
        int sum = 0;
        int count = 0;//记录偶数的个数
        for (int i = 1;i <= 100;i++) {
            if(i%2==0){
                System.out.println(i);
                sum += i;
                count++;
            }
        }
        System.out.println(sum);
        System.out.println(count);
​
        
    }
}

2.while循环

/*
从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入
为0时结束程序。
​
说明:
1 for(;;)
2 while(true)
3 结束循环: 
    循环条件返回false
    循环体中执行break
*/
​
import java.util.Scanner;
class ForWhileTest{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int z = 0,f = 0;
​
        //for循环
        /*for(int i = 1;i != 0;){
            System.out.println("please input a int number:");
            
            int input = scan.nextInt();
            if(input > 0){
                z++;
            }else if(input < 0){
                f++;
            }else{
                i = 0;
            }
        }
        */
        
​
        //while循环
        while(true){
            System.out.println("please input a int number:");
            
            int input = scan.nextInt();
            if(input > 0){
                z++;
            }else if(input < 0){
                f++;
            }else{
                break;
            }
        }
​
        System.out.println("正数:"+z);
        System.out.println("负数:"+f);
    }   
}

3.do-while循环

/*
do-while循环的使用
一、循环结构的四个要素
1.初始化条件
2.循环条件  (boolean)
3.循环体
4.迭代条件
​
二、do-while循环的结构
1;
do{
    3;
    4;
}
while(2);
​
执行过程:1--》3--》4--》2--》3--》4.。。
​
说明:至少执行一次循环体
*/class DoWhileTest{
    public static void main(String[] args){
​
​
        //遍历100内的偶数int num =1;
        do{
            if(num%2 == 0){
                        System.out.println(num);
​
            }
​
            num++;
        }while(num <= 100);
        
    }
}

4.循环的嵌套

/*
嵌套循环的使用
1.嵌套循环:将一个循环结构A声明在另一个循环结构B的循环体中,构成嵌套循环
2.外层循环,内层循环
3.
    1>内层循环结构遍历一遍,只相当于外层循环体执行一次
    2>假设外层循环需要执行m次,内层循环需要执行n次,此时内层循环体执行m*n次
​
4.外层循环控制行数,内层循环控制列数
*/
​
class ForForTest{
    public static void main(String[] args){
        //*****
        for(int i=1;i<=6;i++)
        {
            System.out.print("*");
        }
        System.out.println();
​
​
        /*
        ******
        ******
        ******
        ******
        */
        for(int j = 0;j<=4;j++){
            for(int i=1;i<=6;i++){
                System.out.print("*");
​
            }
            System.out.println();
        }
​
​
​
        /*              j(行号)   i(*的个数)
        *                   1           1   
        **                  2           2
        ***                 3           3
        ****                4           4
        *****               5           5
        */
​
        for(int j = 0;j<=5;j++){
            for(int i=1;i<=j;i++){
                System.out.print("*");
            }
            System.out.println();
        }
​
​
        /*
        ****
        ***
        **
        *
        */
        for(int j = 1;j<=4;j++){
            for(int i=1;i<=5-j;i++){
                System.out.print("*");
            }
            System.out.println();
        }
​
​
        /*
                * 
               * * 
              * * * 
             * * * * 
            * * * * * 
             * * * * 
              * * * 
               * * 
                * 
        */
        //上半部分
        for(int j = 1;j<=5;j++){
            for(int i=1;i<=5-j;i++){
                System.out.print(" ");
            }
            for(int i=1;i<=j;i++){
                System.out.print("* ");
            }
            System.out.println();
        }
        //下半部分
        for(int j = 1;j<=4;j++){
            for(int i=1;i<=j;i++){
                System.out.print(" ");
            }
            for(int i=1;i<=5-j;i++){
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

5.break与continue的使用

/*
break和continue关键字的使用
                使用范围                    
break       switch-casse或循环结构       结束当前循环
​
continue    循环结构中                   结束当次循环
​
return 结束方法
*/
class BreakContinueTest{
    public static void main(String[] args){
        for(int i = 1;i <= 10;i++){
            if(i % 4 == 0){
                break;
            }
            System.out.print(i);
        }
        System.out.println();
​
        for(int i = 1;i <= 10;i++){
            if(i % 4 == 0){
                continue;
            }
            System.out.print(i);
        }
        System.out.println();
​
        //******************************
        
        label:for(int i = 1;i <= 4;i++){
        
            for(int j = 1;j <= 10;j++){
                
                if(j % 4 == 0){
                    //break;//默认跳出包裹此关键字最近的一层循环。
                    //continue;
​
                    //break label;//结束指定标识的一层循环结构
                    continue label;//结束指定标识的一层循环结构当次循环
                }
                
                System.out.print(j);
            }
            
            System.out.println();
        }
    }
}

三、数组

(一)数组概述

1.数组的理解

数组(Array),是多个相同类型数据按一定顺序排列 的集合,并使用一个名字命名,并通过编号的方式 对这些数据进行统一管理。

2.数组的常见概念

  • 数组名
  • 下标(或索引)
  • 元素
  • 数组的长度

3.数组的特点:

  • 数组是有序排列的
  • 数组属于引用数据类型的变量,数组中的元素可以是任何数据类型,包括基本数据类型和引用数据类型
  • 创建数组对象会在内存中开辟一整块连续的空间,而数组名中引用的是这块连续空间的首地址。
  • 数组的长度一旦确定,就不能修改。
  • 直接通过下标(或索引)的方式调用指定位置的元素,速度很快。

4.数组的分类:

  • 按照维度:一维数组、二维数组、三维数组、…
  • 按照元素的数据类型分:基本数据类型元素的数组、引用数据类型元素的数组(即对象数组)

(二)一维数组的使用

1.一维数组的声明和初始化

(1)声明

type var[] 或 type[] var;

例如: int a[]; int[] a1; double b[]; String[] c; //引用类型变量数组

Java语言中声明数组时不能指定其长度(数组中元素的数), 例如: int a[5]; //非法

(2)初始化

动态初始化:数组声明且为数组元素分配空间与赋值的操作分开进行

image.png

静态初始化:在定义数组的同时就为数组元素分配空间并赋值。

image.png

2.一维数组的引用

  • 定义并用运算符new为之分配空间后,才可以引用数组中的每个元素;
  • 数组元素的引用方式:数组名[数组元素下标]
  1. 数组元素下标可以是整型常量或整型表达式。如a[3] , b[i] , c[6i];*
  2. 数组元素下标从0开始;长度为n的数组合法下标取值范围: 0 —>n-1;如int a[]=new int[3]; 可引用的数组元素为a[0]、a[1]、a[2]
  • 每个数组都有一个属性length指明它的长度,例如:a.length 指明数组a的长 度(元素个数)

    数组一旦初始化,其长度是不可变的

    public class ArrayTest {
        public static void main(String[] args) {
            //1.一维数组的声明和初始化
            int num;//声明
            num = 10;//初始化
            int id = 1001;//声明和初始化
            
            //1.1静态初始化:数组的初始化和数组元素的赋值同时进行
            int [] ids;//声明
            ids = new int [] {1001,1002,1003,1004};//数组是引用数据类型,所以赋值时要用new
            
            //1.2动态初始化:数组的初始化和数组元素的赋值分开进行
            String[] names = new String[5];
            
            //总结:数组一旦初始化完成,数组长度就确定了
            
            //2.如何调用数组的指定位置的元素:通过脚标的方式
            //数组的脚标从零开始到数组的长度减一结束
            names[0] = "王三";
            names[1] = "王四";
            names[2] = "王五";
            names[3] = "王六";
            names[4] = "王七";//charAt(0)获取字符串中元素
            
            
            //3.如何获取数组的长度。属性length
            
            System.out.println(names.length);//5
            System.out.println(ids.length);
            
            
            //4.如何遍历数组
            for(int i = 0;i < names.length;i++) {
                System.out.println(names[i]);
            }
            
            for(int j = 0;j < ids.length;j++) {
                System.out.println(ids[j]);
            }