初识数组

数组的大概内容(自学)上篇

数组的创建和赋值

创建:

  1. int [] name = new int [5];

  2. int name [] = new int [5];

  3. int [] name = {1,2.3,4,5};

赋值:

  1. int [] score = {1,2,3};

  2. int [] score = new int [] {1,2,3};

  3. int [] score;//声明

    score = new int []{1,2,3};

键盘赋值
复制代码
package shuzu;
​
import java.util.Scanner;
​
public class shuZuDemo01 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int [] scores = new int [3];
        for (int i = 0; i < scores.length; i++) {
            scores[i] = input.nextInt();
        }
    }
}

数组的遍历

复制代码
package shuzu;
//数组的遍历
public class shuZuDemo02 {
    //定义一个方法
    public static void print(int [] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
    public static void main(String[] args) {
        int [] arr = new int []{1,2,3,4,5};
        print(arr);
        System.out.println();
        //加强for便利更简洁
        int [] arr1 = new int []{1,2,3,4,5,6,7,8,9};
        for (int x : arr1) {
            System.out.print(x + " ");
        }
​
    }
}

最值问题

复制代码
package shuzu;
​
public class shuZuDemo03 {
    public static void main(String[] args) {
        int [] arr = {1,2,3,4,5,6,7,8,9,10};
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println(max);
    }
}

排序算法

冒泡排序

复制代码
package shuzu;
​
public class shuZuDemo04 {
    public static void main(String[] args) {
        int[] arr = {25, 2, 350, 4, 11, 5, 6, 99, 9};
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        for (int x : arr) {
            System.out.print(x+" ");
        }
    }
}

打卡!打卡!打卡!打卡!打卡!

相关推荐
Sam-August3 分钟前
【分布式架构实战】Spring Cloud 与 Dubbo 深度对比:从架构到实战,谁才是微服务的王者?
java·spring cloud·dubbo
麦兜*12 分钟前
MongoDB 常见错误解决方案:从连接失败到主从同步问题
java·数据库·spring boot·redis·mongodb·容器
ytadpole1 小时前
揭秘设计模式:命令模式-告别混乱,打造优雅可扩展的代码
java·设计模式
用户3721574261351 小时前
Java 教程:轻松实现 Excel 与 CSV 互转 (含批量转换)
java
叫我阿柒啊1 小时前
Java全栈开发实战:从基础到微服务的深度解析
java·微服务·kafka·vue3·springboot·jwt·前端开发
凯尔萨厮2 小时前
Java学习笔记三(封装)
java·笔记·学习
霸道流氓气质2 小时前
Java开发中常用CollectionUtils方式,以及Spring中CollectionUtils常用方法示例
java·spring
失散132 小时前
分布式专题——5 大厂Redis高并发缓存架构实战与性能优化
java·redis·分布式·缓存·架构
通达的K2 小时前
Java实战项目演示代码及流的使用
java·开发语言·windows
David爱编程2 小时前
深入 Java synchronized 底层:字节码解析与 MonitorEnter 原理全揭秘
java·后端