一、一维数组
概念:存放多个数据的容器
声明:数据类型[] 数组名;
注意:
元素:数组中的数据
下标/索引:元素在数组中的编号,从0开始
数组是引用数据类型,一旦初始化后,会在内存中开辟一连串连续的空间
数组初始化后,长度是不可以改变的,所以数组没有添加或删除的功能
初始化:
静态初始化:数据由程序员给定,长度由系统自动分配
动态初始化:长度由程序员给定,数据由系统自动分配
整数类型数组的默认值:0 int[] is = new int[10];
浮点数类型数组的默认值:0.0 double[] ds = new double[10];
字符类型数组的默认值:' ' char[] cs = new char[10];
布尔类型数组的默认值:false boolean[] bs = new boolean[10];
引用类型数组的默认值:null String[] ss = new String[10];
静态初始化:一开始就知道数据
静态初始化内存图
动态初始化:一开始就知道数组长度,不知道具体数据
动态初始化内存图
public static void main(String[] args){
//静态初始化1
//String[] names = new String[]{"麻生希","三上悠亚","水野朝阳","桃谷绘里香","明日花绮罗"};
//静态初始化2
//String[] names;
//names = new String[]{"麻生希","三上悠亚","水野朝阳","桃谷绘里香","明日花绮罗"};
//静态初始化3
String[] names = {"麻生希","三上悠亚","水野朝阳","桃谷绘里香","明日花绮罗"};
//设置指定下标上的元素
names[2] = "杨晨";
//获取指定下标上的元素
String str = names[2];
System.out.println("获取指定下标上的元素:" + str);
//获取元素的个数
int length = names.length;
System.out.println("获取元素的个数:" + length);
System.out.println("--------------");
//遍历:从头到尾
//for循环
for(int i = 0;i<names.length;i++){
System.out.println(names[i]);
}
System.out.println("--------------");
//foreach 增强for循环(依次取出数组中的元素,并赋值给name)
for(String name : names){
System.out.println(name);
}
/**
for vs foreach
for中可以使用到下标
foreach中不能使用下标
*/
//注意:ArrayIndexOutOfBoundsException -- 数组下标越界异常
//System.out.println(names[5]);
}
数组案例:声明5个数据的int数组,求出最大值(数据自己写)
注意:
int为基本数据类型
int[] 为引用数据类型
public static void main(String[] args){
int[] is = {35,86,12,66,45};
int max = is[0];
for(int i = 1;i<is.length;i++){
if(max < is[i]){
max = is[i];
}
}
System.out.println("最大值为:" + max);
}
数组的排序
冒泡排序
口诀:
N个数字来排序
两两相比小靠前
外层循环N-1
内层循环N-1-i
选择自学:选择排序、插入排序
int[] is = {10,55,36,89,35,61,1};
for(int i = 0;i<is.length-1;i++){
for(int j=0;j<is.length-1-i;j++){
if(is[j] > is[j+1]){
int temp = is[j];
is[j] = is[j+1];
is[j+1] = temp;
}
}
}
for(int num:is){
System.out.println(num);
}
数组的查找
- 顺序查找
int[] is = {10,55,36,89,35,61,1};
int num = 55;
for(int i = 0;i<is.length;i++){
if(is[i] == num){
System.out.println("找到了");
}
}
- 二分法查找(前提:先排序再查找)
int[] is = {10,55,36,89,35,61,1};
int num = 55;
//排序
Arrays.sort(is);
int start = 0;
int end = is.length-1;
while(start <= end){
int mid = (start+end)/2;
if(num > is[mid]){
start = mid+1;
}else if(num < is[mid]){
end = mid-1;
}else{
System.out.println("找到了");
break;
}
}
数组的复制1
缺点:更改原数组的数据,新数组也会发生改变
String[] names = {"麻生希","爱田奈奈","小峰由衣"};
String[] newNames = names;
names[1] = "杨晨";
for(String str : newNames){
System.out.println(str);
}
数组的复制2
String[] names = {"麻生希","爱田奈奈","小峰由衣"};
String[] newNames = new String[names.length];
for(int i = 0;i<names.length;i++){
newNames[i] = names[i];
}
names[1] = "杨晨";
for(String str : newNames){
System.out.println(str);
}
数组的扩容
String[] names = {"麻生希","爱田奈奈","小峰由衣"};
String[] newNames = new String[names.length*2];
for(int i = 0;i<names.length;i++){
newNames[i] = names[i];
}
names = newNames;
for(String str : names){
System.out.println(str);
}
数组的删除1
缺点:数组的长度变短了
String[] names = {"麻生希","爱田奈奈","小峰由衣"};//0 1 2
String[] newNames = new String[names.length-1];//0 1
int index = 0;
for(String n : names){
if(!n.equals("爱田奈奈")){
newNames[index] = n;
index++;
}
}
names = newNames;
for(String n:names){
System.out.println(n);
}
数组的删除2
String[] names = {"麻生希","爱田奈奈","小峰由衣","水野朝阳","深田咏美"};
for(int i = 1;i<names.length-1;i++){
names[i] = names[i+1];
}
names[names.length-1] = null;
for(String n:names){
System.out.println(n);
}
数组参数和返回值
需求:设计一个方法,传入数组,返回最大值和最小值
public static void main(String[] args){
int[] is = {10,55,36,89,35,61,1};
int[] maxMin = getNum(is);
System.out.println("最大值:" + maxMin[0]);
System.out.println("最小值:" + maxMin[1]);
}
public static int[] getNum(int[] is){
int max = getMax(is);
int min = getMin(is);
return new int[]{max,min};
}
public static int getMax(int[] is){
int max = is[0];
for(int i = 1;i<is.length;i++){
if(max < is[i]){
max = is[i];
}
}
return max;
}
public static int getMin(int[] is){
int min = is[0];
for(int i = 1;i<is.length;i++){
if(min > is[i]){
min = is[i];
}
}
return min;
}
可变参数
含义:可变参数本质意义就是数组
public static void main(String[] args){
/**
可变参数
含义:可变参数本质意义就是数组
*/
int sum = add(1,2,3);//将实参认为是数组的元素,压入到数组中
System.out.println("总和为:" + sum);
}
public static int add(int... is){//可变参数就是数组
int sum = 0;
for(int num:is){
sum+=num;
}
return sum;
}
//规则:可变参数后不能加其他形参
//public static void method(String str,int... is){}
Arrays工具类
Arrays工具类:Java提供的专门操作数组的类
工具类:该类里的方法都是静态的,直接用类名调用
API:Java提供类的使用说明书
int[] is = {10,55,36,89,35,61,1};
//排序
Arrays.sort(is);//1,10,35,36,55,61,89
//查找(返回值:如果该元素在数组中就返回下标,否则返回 -插入点-1)
int index = Arrays.binarySearch(is,69);
System.out.println("查找的元素下标为:" + index);
//替换
Arrays.fill(is,888);
Arrays.fill(is,1,4,666);
//把数组转换为字符串
String isStr = Arrays.toString(is);
System.out.println(isStr);
//拷贝1
int[] newIs1 = Arrays.copyOf(is,10);
System.out.println(Arrays.toString(newIs1));
//拷贝2
int[] newIs2 = Arrays.copyOfRange(is,1,4);
System.out.println(Arrays.toString(newIs2));
超简易学生姓名管理系统
package com.dream.test01;
import java.util.Arrays;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("欢迎进入学生管理系统!");
boolean flag = true;
String name;
while (true) {
System.out.println("请选择功能:");
System.out.println("1-添加姓名 2-删除姓名 3-修改姓名 4-打印所有学生姓名 886-退出系统");
int num = scan.nextInt();
switch (num) {
case 1:
System.out.println("请输入要添加的姓名:");
name = scan.next();
if (add(name)) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
break;
case 2:
System.out.println("请输入要删除的姓名:");
name = scan.next();
if (delete(name)) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
break;
case 3:
System.out.println("请输入要修改的名字:");
name = scan.next();
System.out.println("请输入修改后的名字:");
String newName = scan.next();
if (update(name,newName)) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
break;
case 4:
printName();
break;
case 886:
flag = false;
printName();
break;
default:
System.out.println("输入有误,请重新输...");
break;
}
scan.close();
}
}
//全局变量
static String[] names = new String[3];
static int point = 0;
//添加姓名
public static boolean add(String name) {
//判断姓名合法性
if (name.length()>8) {
System.out.println("名字个数不能大于8");
return false;
}
//查询是否有该学生
if (query(name)!=-1) {
return false;
}
//扩容
if (point ==names.length) {
names = Arrays.copyOf(names, names.length+1);
}
//添加
names[point++]=name;
return true;
}
//删除姓名
public static boolean delete(String name) {
//判断姓名合法性
if (name.length()>8) {
System.out.println("名字个数不能大于8");
return false;
}
int query = query(name);
//查询是否有该学生
if (query==-1) {
return false;
}
//删除
for (int i = query; i < point-1; i++) {
names[i] = names[i+1];
}
point--;
return true;
}
//查询姓名
public static int query(String name) {
for (int i = 0; i < point; i++) {
if (names[i].equals(name)) {
return i;
}
}
return -1;
}
//修改姓名
public static boolean update(String sourceName,String targetName) {
//查询是否有该学生
int query = query(sourceName);
if ( query==-1) {
System.out.println("没有该学生");
return false;
}
//判断是否与原名相同
if (sourceName.equals(targetName)) {
System.out.println("更改名不能与原名相同!");
return false;
}
//判断姓名合法性
if (targetName.length()>8) {
System.out.println("名字个数不能大于8");
return false;
}
//修改名字
names[query(sourceName)] = targetName;
return true;
}
//打印全部学生
public static void printName() {
for (int i = 0; i < point; i++) {
System.out.println(names[i]);
}
}
}
二维数组
含义:二维数组中,包含了多个一维数组
声明:数据类型[] [] 数组名;
初始化:
静态初始化:数据由程序员给定,长度由系统分配
//静态初始化1
String[][] names = new String[][]{{"北岛玲","椎名空","刘坤杰"},{"爱田奈奈","北条麻衣","吉泽明步","三上悠亚"}};
//静态初始化2
String[][] names;
names = new String[][]{{"北岛玲","椎名空","刘坤杰"},{"爱田奈奈","北条麻衣","吉泽明步","三上悠亚"}};
//静态初始化3
String[][] names = {{"北岛玲","椎名空","刘坤杰"},{"爱田奈奈","北条麻衣","吉泽明步","三上悠亚"}};
动态初始化:长度由程序员给定,数据由系统分配
//动态初始化1
String[][] names = new String[2][3];//2个一维数组,每个一维数组分别有3个元素
//动态初始化2
String[][] names;
names = new String[2][3];//2个一维数组,每个一维数组分别有3个元素
基本操作:
//通过下标设置元素
names[1][2] = "明日花绮罗";
//通过下标获取元素
String n = names[1][2];
System.out.println(n);
//获取长度
System.out.println("获取二维数组中一维数组的个数:" + names.length);//2
System.out.println("获取二维数组中第一个一维数组元素的个数:" + names[0].length);//3
System.out.println("获取二维数组中第二个一维数组元素的个数:" + names[1].length);//4
遍历
遍历思路:先取出二维数组中的一维数组,再取出去一维数组中的元素
//遍历1:for循环
for (int i = 0; i < names.length; i++) {
for (int j = 0; j < names[i].length; j++) {
System.out.println(names[i][j]);
}
}
//遍历2:增强for循环
for (String[] ss : names) {
for (String element : ss) {
System.out.println(element);
}
}
简易五子棋
友情提示:eclipes字体设置为宋体才能正常显示
package com.dream.test;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int length = 20;//棋盘长度
String[][] goBang = new String[length][length];//棋盘容器
//坐标的数组
String[] nums = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
//符号
String add = "╋";
String black = "■";
String white = "○";
//初始化棋盘容器里的数据
for (int i = 0; i < goBang.length; i++) {
for (int j = 0; j < goBang[i].length; j++) {
if(j == length-1){//每一行的最后一列
goBang[i][j] = nums[i];
}else if(i == length-1){//最后一行
goBang[i][j] = nums[j];
}else{
goBang[i][j] = add;
}
}
}
//打印棋盘
for (String[] ss : goBang) {
for (String e : ss) {
System.out.print(e);
}
System.out.println();
}
//落子
boolean bool = true;//true-黑子 false-白子
while(true){
System.out.println("请" + ((bool)?"黑":"白") + "子输入坐标:");
int x = scan.nextInt()-1;
int y = scan.nextInt()-1;
//判断坐标是否超出棋盘范围
if(x < 0 || x > length-2 || y < 0 || y > length-2){
System.out.println("坐标超出棋盘范围,请重新输入");
continue;
}
//判断坐标上是否有棋子
if(!goBang[y][x].equals(add)){
System.out.println("坐标上有棋子,请重新输入");
continue;
}
//落子
goBang[y][x] = (bool)?black:white;
//置反
bool = !bool;
//打印棋盘
for (String[] ss : goBang) {
for (String e : ss) {
System.out.print(e);
}
System.out.println();
}
//判断输赢
if (xAxis(x, y, goBang, black) || yAxis(x, y, goBang, black) || xyAxis(x, y, goBang, black) || yxAxis(x, y, goBang, black)) {
System.out.println("恭喜黑子方获胜!");
break;
}
if (xAxis(x, y, goBang, white) || yAxis(x, y, goBang, white) || xyAxis(x, y, goBang, white) || yxAxis(x, y, goBang, white)) {
System.out.println("恭喜白子方获胜!");
break;
}
}
}
//水平方向
public static boolean xAxis(int x,int y,String[][] goBang,String fh){
int length = 20;
int count = 1;
for (int i = x+1; i < length; i++) {
if (goBang[y][i].equals(fh)) {
count++;
}else {
break;
}
}
for (int i = x-1; i >= 0; i--) {
if (goBang[y][i].equals(fh)) {
count++;
}else {
break;
}
}
//System.out.println(count);
if (count >= 5) {
return true;
}
return false;
}
//垂直方向
public static boolean yAxis(int x,int y,String[][] goBang,String fh){
int length = 20;
int count = 1;
for (int i = y+1; i < length; i++) {
if (goBang[i][x].equals(fh)) {
count++;
}else {
break;
}
}
for (int i = y-1; i >= 0; i--) {
if (goBang[i][x].equals(fh)) {
count++;
}else {
break;
}
}
//System.out.println(count);
if (count >= 5) {
return true;
}
return false;
}
//左上到右下方向
public static boolean xyAxis(int x,int y,String[][] goBang,String fh){
int length = 20;
int count = 1;
int index = 1;
for (int i = x+1; i < length; i++) {
if (goBang[y+index][i].equals(fh)) {
count++;
}else {
break;
}
index++;
}
index=1;
for (int i = x-1; i >= 0; i--) {
if (y-index<0) {
break;
}
if (goBang[y-index][i].equals(fh)) {
count++;
}else {
break;
}
index++;
}
//System.out.println(count);
if (count >= 5) {
return true;
}
return false;
}
//右上到左下
public static boolean yxAxis(int x,int y,String[][] goBang,String fh){
int length = 20;
int count = 1;
int index = 1;
for (int i = x+1; i < length; i++) {
if (y-index<0) {
break;
}
if (goBang[y-index][i].equals(fh)) {
count++;
}else {
break;
}
index++;
}
index=1;
for (int i = x-1; i >= 0; i--) {
if (goBang[y+index][i].equals(fh)) {
count++;
}else {
break;
}
index++;
}
System.out.println(count);
if (count >= 5) {
return true;
}
return false;
}
}