初识数组

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

数组的创建和赋值

创建:

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

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

相关推荐
少许极端7 分钟前
算法奇妙屋(十)-队列+宽搜(BFS)
java·数据结构·算法·bfs·宽度优先·队列
程序员卷卷狗1 小时前
JVM 内存结构与 GC 调优全景图
java·开发语言·jvm
foxbillcsdn2 小时前
《Redis应用实例》Java实现(28):栈
java·redis
serendipity_hky2 小时前
【微服务 - easy视频 | day01】准备工具+gateway网关及路由至内部服务
java·微服务·架构·gateway·springcloud
Geoking.2 小时前
【Java】Java 中 @Resource 与 @Autowired 的区别详解
java·开发语言
weixin_441455263 小时前
说说Java有哪些集合类
java·开发语言
合作小小程序员小小店3 小时前
web网页开发,在线%台球俱乐部管理%系统,基于Idea,html,css,jQuery,jsp,java,ssm,mysql。
java·前端·jdk·intellij-idea·jquery·web
程序定小飞3 小时前
基于springboot的作业管理系统设计与实现
java·开发语言·spring boot·后端·spring
晓庆的故事簿3 小时前
windows下载和使用minio,结合java和vue上传文件
java·开发语言