数组定义:
数组(array): 最基本的一种数据结构 - 数据的排列方式, 数组是第一种引用类型
可以存储多个变量的容器。
初始化
: int[] arr = {元素}; – 直接确定元素内容和长度
int[] arr = new int[] {元素} – 直接确定元素内容和长度
int[] arr = new int[num] – 确定长度, 内容默认值
数组属性:长度
arr.length: 不可变
数组元素: 通过下标/索引 index
index范围: 0 ~ length-1
arr[index] 访问\赋值。
数组遍历: for循环
JVM内存空间:
方法区: 存储类相关的信息
栈: 临时变量, 局部变量
堆: 引用类型的真正数据, 对象
类加载
: 将字节码(class文件)中的类信息读取到JVM方法区中由类加载器完成。
方法:
函数/过程, 包含了一些代码
主方法
语法/结构:
修饰词 方法返回值类型 方法名 (参数列表) {方法体}
public static void/int/int[]/boolean/String main/随意 int a, int b 一些代码
参数: 也叫临时变量, 类型 变量名
参数列表可以为空, 也可以多个, 多个参数之间用’,'隔开。
有返回值的方法, 需要通过return返回结果
return表示方法结束, 没有返回值的方法, 也可以使用return
案例1
: 猜字母游戏:随机生成5个字母 猜字母
提示: 几个字母正确, 其中位置正确的有几个。
package day03;
import java.util.Arrays;
import java.util.Scanner;
public class day03Demo08 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
char[] arrChar = randomChars();
System.out.println(Arrays.toString(arrChar));
while (true) {
String s = console.next();
char[] guess = parseChar(s);
int[] res = compare(arrChar, guess);
System.out.print("字母正确个数" + res[0]);
System.out.println(",其中位置正确个数" + res[1]);
if (res[1] == 5) {
break;
}
}
}
public static int[] compare ( char[] answer, char[] guess){
int[] result = new int[2];
for (int i = 0; i < answer.length; i++) {
for (int j = 0; j < guess.length; j++) {
if (answer[i] == guess[j]) {
result[0]++;
if (i == j) {
result[1]++;
}
}
}
}
return result;
}
public static char[] randomChars () {
char[] chars = new char[5];
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) (Math.random() * 26 + 'a');
for (int j = 0; j < i; j++) {
if (chars[i] == chars[j]) {
i--;
break;
}
}
}
return chars;
}
public static char[] parseChar (String str){
char[] arrChar = new char[5];
for (int i = 0; i < arrChar.length; i++) {
arrChar[i] = str.charAt(i);
System.out.println(arrChar[i]);
}
return arrChar;
}
}
结果:
常用的方法类型
Scanner
int nextInt(): 获取控制台内容, 并且转换成int值返回
double nextDouble(): 获取控制台内容,并且转换成double返回
String next(): 获取控制台字符串, 并返回(遇到回车或者空格读取结束)
String nextLine(): 获取控制台一整行字符串,并返回(遇到回车结束)
System
void print(内容) -> 打印, 不换行
void println(空/内容) -> 打印完, 换行
package com.zzxx.api;
public class Demo01Print {
public static void main(String[] args) {
// 可以不写参数, 就是打印换行
System.out.println();
// 不换行, 必须给参数
System.out.print("\n");
System.out.println(2);
// 常见的转义字符 \n->回车 \r->换行 \t->制表符
// \\->\本身
}
}
Math
double random(): 返回一个[0,1)随机数
double pow(double d1, double d2): 返回 d1 的 d2 次幂
package day03;
public class day03Demo06 {
public static void main(String[]args){
int d=(int)Math.pow(5,3);/*5的3次方*/
System.out.println(d);
}
}
**输入字符串,输出字符。**
package day03;
import java.util.Scanner;
public class day03Demo07 {
public static void main(String[] args) {
Scanner console=new Scanner(System.in);
String str=console.next();
char[] arrChar=new char[5];
for(int i=0;i<arrChar.length;i++){
arrChar[i]=str.charAt(i);
System.out.println(arrChar[i]);
}
}
}
结果:
输出随机大写字母:
package day03;
public class day03Demo05 {
public static void main(String[] args) {
char d = (char) (Math.random() * 26 + 65);
System.out.println(d);
}
}
/* 随机生成一个大写字母 */
转义字符: \n:回车 \r:换行 \t:制表符 \:\本身
String:
char charAt(int index): 返回index索引位置对应的字符
Arrays:
String toString(数组): 将数组变成字符串格式返回
命名规则
方法名: 首字母小写、第二个单词开始首字母大写 - 驼峰命名法
长度不限, 但是一定要可读性好