初识数组

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

数组的创建和赋值

创建:

  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+" ");
        }
    }
}

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

相关推荐
SeaDhdhdhdhdh8 小时前
MCP Server 搭建与使用指南
java·ai·agent·mcp
YH55269848 小时前
GPT‑5.6 Sol 原本支持 1M 上下文,Codex 现已放开此前限制,如何看待这次调整?
java·jvm·人工智能·gpt·算法·chatgpt
AI_小站9 小时前
刚面完百度的 Agent 开发岗,我才发现:世界就是个巨大的草台班子
java·开发语言·人工智能·spring·百度·langchain
许彰午9 小时前
03-三种开发模式
java·架构
涟漪海洋10 小时前
创建最新的JDK25镜像,非root环境启动
java
Rain的Java大神之路10 小时前
短信接口被狂刷怎么处理
java·运维·后端·web安全·面试·架构·xss
2602_9599609211 小时前
谢飞机面试大厂:Spring Boot、JVM、Redis、Kafka、微服务与音视频搜索场景求生实录
java·jvm·spring boot·redis·面试题
Zane19941211 小时前
CAS 与原子类:Java 如何实现无锁编程
java·开发语言
略略略咯咯11 小时前
AI玩具-study
java
xier_ran12 小时前
【infra之路】GPU 存储层次总结:L1 / L2 / HBM
java·网络·数据库