数组的基本概念及作用
java数组概念 数组是一组相同数据类型元素的集合,是一个容器 数组本身是引用数据类型,是一个对象 数组可以存储基本数据类型,也可以存储引用数据类型 数组创建时必须指定长度,且长度不可变, 数组中每个元素空间是连续的。
数组的创建
数组的声明的两种方式:
1. 数据类型 [] 数组名字 例如:int [] a;
2.数据类型 数组的名字 [] 例如: int a [];
注意: 在Java语言中两种声明方法没有任何区别,但是建议大家用第一种, 避免混淆a的数据类型
● 数组创建:
1.创建一个容量为5的一个数组,使用默认值对其进行初始化 int[] ary0 = new int[5];
2.创建数组同时将其用指定的值初始化,有几个元素容量就是多少 int[] ary1 = new int[]{1, 2, 3, 4, 5};
3.方式2还可以简化为下面写法 int[] ary2 = {1, 2, 3, 4, 5};
数组的访问与迭代
数组元素的访问:
数组名[索引] 例如:a[0],a[1];
注意: 数组的索引从0开始。 索引的数据类型是整数(int) 索引最大值和数组长度始终差1
获取数组长度
数组名.length
数组迭代的两种方式:
第一种:for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
数组的迭代
for(int i =0;i<b1.length;i++)
{
System.out.println(b1[i]);
}
第二种:增强for循环
int [] b1 = new int []{1,2,3,4,5,6,7};
for(数组元素的类型 临时变量名字 :数组的名字){
System.out.println(临时变量名字 );
}
即:
for(int x : b1){
System.out.println(x);
}
二维数组
二维数组的定义:
数组的数组,二维数组的每一个元素是一个一维数组
例如:
int [][]a = {{1,2,3},{1,2,3},{1,2,3}};
●二维数组的声明:
int [][] a;
int a2[][];
注意:建议用第一种,不容易混淆a的数据类型
● 数组创建
int [][]a = new int[][]{{1,2,3},{1,2,3},{1,2,3}};
int [] [] b = {{1,2,3},{1,2,3},{1,2,3}};
int [][] c = new int[3][5];
int[][] arr = new int[3][5];
定义了一个整型的二维数组 ,这个二维数组有3个一维数组,每一个一维数组包含5个元素.
●二维数组的遍历:
利用二维数组完成五子棋的游戏
public class WZQ {
/*
1.提供一个启动五子棋游戏的方法
初始化棋盘方法
打印棋盘方法
2.开始下棋,黑白棋交替下棋while(true)
3.判断坐标是否合法,是否重复chick
4.判断输赢iswin
*/
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];
static String[] num = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖"};
static String line = "十";
static Scanner scanner =new Scanner(System.in);
//启动五子棋游戏
public static void startGame(){
init();
print();
play();
}
//初始化棋盘
public static void init(){
for (int i = 0; i < qp.length; i++) {
for (int j = 0; j <qp[i].length; j++) {
qp[i][j]=line;
if(j== qp.length-1)
{
qp[i][j]=num[i];
}
if(i== qp.length-1)
{
qp[i][j]=num[j];
}
}
}
}
//打印棋盘
public static void print(){
for (int i = 0; i < qp.length ; i++) {
for (int j = 0; j < qp.length ; j++) {
System.out.print(qp[i][j]);
}
System.out.println();
}
}
public static void play()
{
boolean flag=true;
while(true)
{
if(flag)
{
System.out.println("黑方下棋");
System.out.println("请输入行:");
int row=scanner.nextInt()-1;
System.out.println("请输入列:");
int column=scanner.nextInt()-1;
boolean res =chick(row,column);//判胜方法
if(res)
{
qp[row][column]=black;
print();
}
boolean result=isWin(qp,row,column);
if(result)
{
System.out.println("黑方获胜");
break;
}
flag =false;
}
else
{
System.out.println("白方下棋");
System.out.println("请输入行:");
int row=scanner.nextInt()-1;
System.out.println("请输入列:");
int column=scanner.nextInt()-1;
boolean res =chick(row,column);
if(res)
{
qp[row][column]=white;
print();
}
boolean result=isWin(qp,row,column);
if(result)
{
System.out.println("白方获胜");
break;
}
flag =true;
}
}
}
public static boolean chick(int row,int column)
{
if (row < 0 || column < 0 || row > qp.length || column > qp[0].length)
{
System.out.println("越界");
return false;
}
if (qp[row][column] != line)
{
System.out.println("重复");
return false;
}
else
return true;
}
public static int left_right(String[][]qp, int r, int c) {
int count = 0;
String chessNum = qp[r][c];
for (int i = c + 1; i < qp[r].length; i++) {
if (chessNum == qp[r][i]) {
count++;
} else {
break;
}
}
//向左
for(int i = c-1;i>=0;i--){
if(chessNum == qp[r][i]){
count++;
}else{
break;
}
}
count++;
System.out.println(count);
return count;
}
public static int up_down(String[][]qp, int r, int c){
int count = 0;
String chessNum = qp[r][c];
//向上
for(int i = c-1; i>=0;i--){
if(chessNum == qp[i][c]){
count++;
}else{
break;
}
}
//向下
for(int i = c+1;i<qp.length;i++){
if(chessNum == qp[i][c]){
count++;
}else{
break;
}
}
count++;
System.out.println(count);
return count;
}
public static int leftUp_RightDown(String [][]qp, int r, int c){
int count = 0;
String chessNum = qp[r][c];
//右下
for(int i = r+1,j=c+1;i<qp.length&&j<qp[r].length;i++,j++){
if(chessNum == qp[i][j]){
count++;
}else{
break;
}
}
//左上
for(int i=r-1,j=c-1;i>=0&&j>=0;i--,j--){
if(chessNum == qp[i][j]){
count++;
}else{
break;
}
}
count++;
System.out.println(count);
return count;
}
public static int leftDown_RightUp(String [][]qp, int r, int c){
int count = 0;
String chessNum = qp[r][c];
//左下
for(int i = r+1,j=c-1;i<qp.length&&j>=0;i++,j--){
if(chessNum == qp[i][j]){
count++;
}else{
break;
}
}
//右上
for(int i=r-1,j=c+1;i>=0&&j<qp[r].length;i--,j++){
if(chessNum == qp[i][j]){
count++;
}else{
break;
}
}
count++;
System.out.println(count);
return count;
}
public static boolean isWin(String[][] chessArray, int r, int c){
if(left_right (chessArray, r, c) >4 || up_down (chessArray, r, c) >4 || leftUp_RightDown(chessArray, r, c) >4)
{
return true;
}
return false;
}
}
public class StartGame {
public static void main(String[] args) {
WZQ.startGame();
}
}