java --数组基础

数组的初始化:
数组的类型是固定的,只能存相同类型的数据
有3种初始化的方式。
int【】arr={元素};直接确定元素内容和长度
int【】arr=new int【】{元素} 直接确定元素内容和长度
int【】arr=new int【num】 确定长度
数组属性: 长度
arr.length: 不可变
数组元素: 通过下标/索引 index
index范围: 0 ~ length-1
arr[index] 访问\赋值
数组遍历: for循环
JVM内存空间:
方法区: 存储类相关的信息
栈: 临时变量, 局部变量
堆: 引用类型的真正数据, 对象
1.Math数学类讲解

public class z {
        public static void main(String[] args) {
            // random() -> [0,1)
            double rand = Math.random();
            System.out.println(rand);
            // [0,20)
            double r1 = Math.random() * 20;
            System.out.println(r1);

            // [0, 100) 整数
            int r2 = (int) (Math.random() * 100);
            System.out.println(r2);

            // [65, 90]   [0,1)->[0,26)->[65,91)
            int r3 = (int) (Math.random() * 26 + 'a');
            // 就是随机大写字母
            char c = (char) (Math.random() * 26 + 65);
            char c1 = (char) (Math.random() * 26 + 'A');

            System.out.println(c);
            System.out.println(c1);

            System.out.println("---------");
            // double Math.pow(double, double)   3 ^ 6
            int pow = (int) Math.pow(2, 10);
            System.out.println(pow);
        }
    }
Scanner console = new Scanner(System.in);
// 遇到回车, 才读取结束
String s1 = console.nextLine();
    System.out.println("字符串: " + s1);

int a = console.nextInt();
    System.out.println("整数: " + a);

double d = console.nextDouble();
    System.out.println("小数: " + d);

// 遇到空格或者回车, 就读取结束
String s = console.next();
    System.out.println("next字符串: " + s);

3.方法的执行过程

public class Demo02MethodProcess {
    public static void main(String[] args) {
        // 调用方法
        int c = sum(4, 6);
        System.out.println(c);
    }
    /*
        计算两个整数的和, 并且返回
     */
    public static int sum(int a, int b) {
        int sum = a + b;
        return sum;
    }
}

4.随机排数

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class RandomSort {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(RandomSort.class.getResourceAsStream("name.txt")));
        String str ;
        List<String> names = new ArrayList<>();
        while((str=br.readLine())!=null){
            names.add(str);
        }
        Collections.shuffle(names);
        names.stream().forEach((name) -> System.out.println(name));
    }
}

常用方法:
  Scanner
    int nextInt(): 获取控制台内容, 并且转换成int值返回
    double nextDouble(): 获取控制台内容,并且转换成double返回
    String next(): 获取控制台字符串, 并返回(遇到回车或者空格读取结束)
    String nextLine(): 获取控制台一整行字符串,并返回(遇到回车结束)
  System
    void print(内容) -> 打印, 不换行
    void println(空/内容) -> 打印完, 换行
  Math
    double random(): 返回一个[0,1)随机数
    double pow(double d1, double d2): 返回 d1 的 d2 次幂
  转义字符: \n:回车  \r:换行  \t:制表符  \:\本身
  String:
    char charAt(int index): 返回index索引位置对应的字符
  Arrays:
    String toString(数组): 将数组变成字符串格式返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值