Java数组学习
ArrayDemo1
package com.yuan.array;
public class ArrayDemo1 {
//变量的类型 变量的名字 = 变量的值;
//数组类型
//数组的长度是确定的,一旦被创建,大小就是不可以被改变的。
public static void main(String[] args) {
int[] nums;//1.声明了一个数组 int nums[];第二种定义方式(C和C++程序员常用)
nums = new int[10];//2.创建了一个数组,这里面可以存放10个int类型的数字
int[] nums2 = new int[10];//可以将两步综合成一步
//3.给数组元素中赋值(int类型默认值是0,String默认值是NULL)
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
nums[3] = 4;
nums[4] = 5;
nums[5] = 6;
nums[6] = 7;
nums[7] = 8;
nums[8] = 9;
nums[9] = 10;
//计算所有元素的和
int sum=0;
//获取数组长度:array.length
for (int i = 0; i < nums.length; i++) {
sum = sum +nums[i];
}
System.out.println("总和为"+sum);
}
}
ArrayDemo2
package com.yuan.array;
public class ArrayDemo2 {
public static void main(String[] args) {
//静态初始化
int[] a ={1,2,3,4,5,6,7,8};
System.out.println(a[0]);
//引用类型
Man[] mans = {new Man(),new Man()};//Man是一个外面的类,没有写内容
//动态初始化:包含默认初始化
int [] b= new int[10];
b[0]=10;
System.out.println(b[0]);
}
}
ArrayDemo3
package com.yuan.array;
public class ArrayDemo3 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
//打印全部的数据元素
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
System.out.println("=========");
//计算所有元素的总和
int sum =0;
for (int i = 0; i < arrays.length; i++) {
sum = sum +arrays[i];
}
System.out.println("sum="+sum);
System.out.println("=========");
//查找最大元素
int max = arrays[0];
for (int i = 1; i < arrays.length; i++) {
if(arrays[i]>max){
max = arrays[i];
}
}
System.out.println("max="+max);
}
}
ArrayDemo4
package com.yuan.array;
public class ArrayDemo4 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
// //JDK1.5,没有下标,取不到下标
// //for(数组里面的每一个值:数组)
// for (int array1 : arrays) {
// System.out.println(array1);
// }
// printArray(arrays);
int[] reverse = reverse(arrays);
printArray(reverse);
}
//打印数组元素
//static是为了方便直接调用
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
//反转数据
// public static int[] reverse(int[] arrays){
// int[] result = new int[arrays.length];
//
// //反转的操作
// for (int i = 0; i < arrays.length; i++) {
// result[i] = arrays[arrays.length-i-1];
// }
//
//
// return result;
// }
public static int[] reverse(int[] arrays){
int[] result = new int[arrays.length];
//反转的操作
for (int i = 0,j=result.length-1; i < arrays.length; i++,j--) {
result[j] = arrays[i];
}
return result;
}
}
/*
1.普通的for循环(最常用)
2.For-Each循环(用于打印一些结果)
3.数组作方法入参(可以对数组进行一些操作)
4.数组作返回值(返回新的数组)
*/
ArrayDemo5
package com.yuan.array;
import javax.xml.transform.Source;
public class ArrayDemo5 {
public static void main(String[] args) {
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
public static void printArray(int[] arrays){
for (int i = 0; i < arrays.length; i++) {
System.out.print(arrays[i]+" ");
}
}
}
ArrayDemo6
package com.yuan.array;
import java.util.Arrays;
public class ArrayDemo6 {
public static void main(String[] args) {
int[] a ={1,2,3,4,5,1561,6115,12,31215,6};
//System.out.println(a);//[I@1b6d3586
//打印数组元素Arrays.toString
System.out.println(Arrays.toString(a));
printArray(a);
System.out.println();
Arrays.sort(a);//数组进行排序
System.out.println(Arrays.toString(a));
Arrays.fill(a,0);
System.out.println(Arrays.toString(a));
}
public static void printArray(int[] a){
for (int i = 0; i < a.length; i++) {
if (i==0){
System.out.print("[");
}
if (i==a.length-1){
System.out.print(a[i]+"]");
}else {
System.out.print(a[i]+", ");
}
}
}
}
ArrayDemo7
package com.yuan.array;
import java.util.Arrays;
public class ArrayDemo7 {
public static void main(String[] args) {
int[] a ={1,4,56,24,95,123,45,84,64};
int[] sort = sort(a);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(sort));
}
// 冒泡排序
//1.比较数组中两个相邻的元素,如果第一个数比第二个数大,我们就交换位置
//2.每一次比较,都会产生出最大或者最小的元素
//3.下一轮可以少排一次序
//4.依次循环直到结束
public static int[] sort(int[] array){
//临时变量
int temp = 0;
//外层循环判断走多少次
for (int i = 0; i < array.length-1; i++) {
boolean flag;//通过flag标识位较少没有意义的比较
//内层循环比较两个数
for (int j = 0; j < array.length-1-i; j++) {
if(array[j+1]<array[j]){
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
if(flag = false){
break;
}
}
return array;
}
}
ArrayDemo8
package com.yuan.array;
public class ArrayDemo8 {
public static void main(String[] args) {
//1.创建一个二维数组 11*11 0没有棋子 1黑棋 2白棋
int[][] array1 = new int[11][11];
array1[1][2] = 1;
array1[2][3] = 2;
//输出原始的数组
System.out.println("输出原始的数组");
for (int[] ints : array1) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
System.out.println("=============");
//转换为稀疏数组保存
//获取有效值的个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if(array1[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值的个数"+sum);
//2.创建一个稀疏数组的数组
int[][] array2 = new int[sum+1][3];
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=sum;
//遍历二维数组,将非零的值,放在稀疏数组中
int count = 0;
for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array1[i].length; j++) {
if (array1[i][j]!=0){
count++;
array2[count][0] = i;
array2[count][1] = j;
array2[count][2] = array1[i][j];
}
}
}
//输出稀疏数组
System.out.println("稀疏数组");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i][0]+"\t"
+array2[i][0]+"\t"
+array2[i][2]+"\t");
}
System.out.println("=============");
System.out.println("还原");
//1.读取稀疏数组
int[][] array3 = new int [array2[0][0]][array2[0][1]];
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] = array2[i][2];
}
//3.打印
System.out.println("输出还原的数组");
for (int[] ints : array3) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}
FiveInARow
package com.yuan;
import java.util.Scanner;
import java.io.*;
public class FiveInARow {
private static final int BOARD_SIZE = 15;
private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE];
private static char currentPlayer = 'X';
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = 0;
System.out.println("欢迎来到五子棋游戏!");
while (true) {
System.out.println("请选择:");
System.out.println("1. 开始新游戏");
System.out.println("2. 续上一盘");
System.out.println("3. 退出游戏");
choice = scanner.nextInt();
if (choice == 1) {
newGame();
play(scanner);
} else if (choice == 2) {
if (!loadGame()) {
System.out.println("续上一盘失败。");
continue;
}
play(scanner);
} else if (choice == 3) {
System.out.println("再见!");
break;
} else {
System.out.println("输入无效,请重新输入。");
}
}
}
private static void newGame() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = '-';
}
}
currentPlayer = 'X';
}
private static void play(Scanner scanner) {
int row, col;
while (true) {
printBoard();
System.out.println("请 " + currentPlayer + " 玩家输入坐标(行列,以空格分隔):");
row = scanner.nextInt();
col = scanner.nextInt();
if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {
System.out.println("输入无效,请重新输入。");
continue;
}
if (board[row][col] != '-') {
System.out.println("该位置已经有棋子了,请重新输入。");
continue;
}
board[row][col] = currentPlayer;
if (checkWin(row, col)) {
System.out.println("恭喜 " + currentPlayer + " 玩家获胜!");
printBoard();
break;
}
if (checkTie()) {
System.out.println("平局!");
printBoard();
break;
}
currentPlayer = currentPlayer == 'X' ? 'O' : 'X';
}
}
private static boolean checkWin(int row, int col) {
char c = board[row][col];
int count = 1;
// 水平方向
for (int i = col - 1; i >= 0 && board[row][i] == c; i--) {
count++;
}
for (int i = col + 1; i < BOARD_SIZE && board[row][i] == c; i++) {
count++;
}
if (count >= 5) {
return true;
}
// 垂直方向
count = 1;
for (int i = row - 1; i >= 0 && board[i][col] == c; i--) {
count++;
}
for (int i = row + 1; i < BOARD_SIZE && board[i][col] == c; i++) {
count++;
}
if (count >= 5) {
return true;
}
// 左上到右下方向
count = 1;
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0 && board[i][j] == c; i--, j--) {
count++;
}
for (int i = row + 1, j = col + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == c; i++, j++) {
count++;
}
if (count >= 5) {
return true;
}
// 右上到左下方向
count = 1;
for (int i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == c; i--, j++) {
count++;
}
for (int i = row + 1, j = col - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == c; i++, j--) {
count++;
}
if (count >= 5) {
return true;
}
return false;
}
private static boolean checkTie() {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
private static void printBoard() {
System.out.print(" ");
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
}
System.out.println();
for (int i = 0; i < BOARD_SIZE; i++) {
System.out.print(i + " ");
for (int j = 0; j < BOARD_SIZE; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
}
private static boolean saveGame() {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("save.txt"));
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
writer.write(board[i][j]);
}
writer.newLine();
}
writer.write(currentPlayer);
writer.newLine();
writer.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
private static boolean loadGame() {
try {
BufferedReader reader = new BufferedReader(new FileReader("save.txt"));
for (int i = 0; i < BOARD_SIZE; i++) {
String line = reader.readLine();
if (line == null || line.length() != BOARD_SIZE) {
return false;
}
for (int j = 0; j < BOARD_SIZE; j++) {
board[i][j] = line.charAt(j);
}
}
String line = reader.readLine();
if (line == null || line.length() != 1) {
return false;
}
currentPlayer = line.charAt(0);
reader.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
}
标签:Java,int,System,学习,++,数组,arrays,println,out
From: https://www.cnblogs.com/codehuman/p/17150562.html