初识数组

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

数组的创建和赋值

创建:

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

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

相关推荐
risc12345618 分钟前
Elasticsearch 线程池
java·大数据·elasticsearch
NE_STOP1 小时前
SpringBoot--如何整体读取多个配置属性及其相关操作
java·spring
apihz1 小时前
通用图片搜索-搜狗源免费API接口使用指南
android·java·python·php·音视频
风象南2 小时前
基于 SpringBoot 的 REST API 与 RPC 调用的统一封装
java·spring boot·后端
素雪风华2 小时前
Jenkins+Gitee+Docker容器化部署
java·docker·gitee·jenkins·springboot·持续部署
用户40315986396632 小时前
带 WriteBuffer 的内存读写操作
java·算法
岁忧2 小时前
(LeetCode 面试经典 150 题 ) 209. 长度最小的子数组(双指针)
java·c++·算法·leetcode·面试·go
码银3 小时前
基于Java的Markdown到Word文档转换工具的实现
java·word
Mr_Xuhhh3 小时前
QWidget的属性
java·数据库·c++·qt·系统架构
小张在编程3 小时前
Java设计模式实战:备忘录模式与状态机模式的“状态管理”双雄
java·设计模式·备忘录模式