二維數組
如果一個數組中全是一維數組,那麼這個數組就是二維數組
整合寫:
int[][] arr = { {1,2,3} , {4,5,6} , {7,8,9} , {10,11,12} };
分步寫:
int[] arr1 = {1,2,3};
int[] arr2 = {4,5,6};
int[] arr3 = {7,8,9};
int[] arr4 = {10,11,12};
int[][] array = {arr1, arr2, arr3, arr4 };
- 如何識別二維數組中的元素、角標
數組和角標 | 輸出值 |
---|---|
System.out.println(“二位數組:” + array); | [[I@7852e922 |
System.out.println(“二位數組中的第一個一維數組:” + array[0]); | [I@4e25154f |
System.out.println(“二位數組中的第一個一維數組:” + array[1][1]); | 5 |
System.out.println(“二位數組中的第一個一維數組:” + array[2][1]); | 8 |
- 二維數組的遍歷過程
外層循環遍歷:一維數組;
內層數組遍歷:一維數組中的元素;
- 格式
for (int i = 0; i < array.length; i++) { //二維數組中有幾個一維數組
for (int j = 0; j < array[i].length; j++) { //一維數組的長度
System.out.print(array[i][j] + " ");
}
}
- 動態初始化
格式
int[][] arr = new int[3][4];
//[3]:代表二維數組中有3個一維數組
//[4]:每個一維數組中有4個數字
方法
- 格式
修飾符 返回類型(有返回值寫返回數據類型,沒返回值寫void) 方法名 (參數列表)
//參數列表(沒有參數的情況可以不寫):類型名字 取個名字
{
方法體......
//如果這個方法有返回值類型 最後一行寫return
return + 對應返回值類型的值;
}
- 方法定義的位置
不能在別的方法裏,必須定義在其他同級的位置,定義在類裏面 - 方法的作用
- 簡化代碼(過程型方法)
- 代碼反覆出現 ,提高代碼複用性(功能型方法)
- 創建方法
- 無返回值、無參數
//方法welcome
public static void welcome(){
System.out.println("hello world");
}
//主方法
public static void main(String[] args){
//調用welcome方法
welcome();
}
- 有返回值、有參數
//方法add
public static int add(int a ,int b){
int c = a+b;
return c;
}
//主方法
public static void main(String[] args){
int x = 20;
int y = 10;
//調用add方法,把方法return的值賦值給sum
int sum = add(x,y);
//輸出x+y
System.out.println(sum);
}
- 方法中的元素
參數列表中的參數:
形式參數 形參:寫方法的時候
實際參數 實參:實際傳入方法參數列表的參數
快捷鍵
- 快捷導包:ctrl+shift+o
練習
- 在main方法中根據鍵盤輸入一個1-100之間的數字n,另外寫一個方法showLove,傳入這個n,循環n次"我愛你"
package test;
public class ShowLove {
public static void main(String[] args) {
showLove(6);
}
public static void showLove(int a) {
for (int i = 0; i < a; i++) {
System.out.println("我愛你");
}
}
}
- 在main方法中隨便定義一個字符串,另外寫一個方法printString,傳入這個字符串,此方法專門負責將字符串打印出來。
package test;
public class PrintString {
public static void main(String[] args) {
printString("我是字符串");
}
public static void printString(String a) {
System.out.println(a);
}
}
- 在main方法中定義一個數組,寫一個方法showIntArr,專門用來遍歷int類型的一維數組。
package test;
public class ShowIntArr {
public static void main(String[] args) {
int[] array = { 1, 2, 3 };
showIntArr(array);
}
public static void showIntArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
- 寫一個方法showCharArr,專門用來遍歷char類型的一維數組。
package test;
public class ShowCharArr {
public static void main(String[] args) {
char[] cha = { '我', '是', '字', '符' };
showCharArr(cha);
}
public static void showCharArr(char[] cha) {
for (int i = 0; i < cha.length; i++) {
System.out.print(cha[i] + " ");
}
}
}
- 寫一個方法showStringArr,專門用來遍歷String類型的一維數組。
package test;
public class ShowStringArr {
public static void main(String[] args) {
String[] str = { "我是", "字符", "串" };
showStringArr(str);
}
public static void showStringArr(String[] a) {
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
- 寫一個方法專門showCharArr2,用來遍歷char類型的二維數組
package test;
public class ShowCharArr2 {
public static void main(String[] args) {
char[][] ch = { { '1', '2' }, { '3', '4' } };
showCharArr2(ch);
}
public static void showCharArr2(char[][] cha) {
for (int i = 0; i < cha.length; i++) {
for (int j = 0; j < cha[i].length; j++) {
System.out.print(cha[i][j] + " ");
}
System.out.println();
}
}
}
- 在main方法中有一個int類型一維數組,寫一個方法getMax,用於獲取數組中的最大值,調用方法之後將返回的最大值賦值給一個變量a,把a打印出來。
package test;
public class GetMax {
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5 };
int a = getMax(array);
System.out.println("最大的值爲" + a);
}
public static int getMax(int[] arr) {
int max = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
- 根據鍵盤輸入一個數字(1-7),寫一個方法getWeek,傳入這個數字,專門來獲取這個數字所對應的星期幾字符串,之後調用這個方法來獲取返回的字符串並賦值給s,打印出這個s。
package test;
import java.util.Scanner;
public class GetWeek {
public static void main(String[] args) {
System.out.println("輸入一個數字(1-7)");
Scanner ss = new Scanner(System.in);
int a = ss.nextInt()-1;
int s = getWeek(a);
System.out.println("傳出的s爲:"+s);
}
public static int getWeek(int b) {
String[] week = {"星期一","星期二","星期三","星期四","星期五","星期六","星期七"};
System.out.println("今天爲" + week[b]);
return b;
}
}
- 在main方法中定義一個int類型的一維數組,根據鍵盤輸入一個數字,寫一個方法addNum,傳入這個數字和數組,把這個數字加到這個數組的末尾。之後在main方法中調用addNum的方法把返回出來的數組賦值給原來的數組,並遍歷原來的數組。
package test;
import java.util.Scanner;
public class AddNum {
public static void main(String[] args) {
int[] array = { 1,2,3};
System.out.println("請輸入一個數字:");
int key = s.nextInt();
array = addNum( array,key );
System.out.println("遍歷原數組:");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
public static int[] addNum(int[] arr, int a) {
//創建一個新的數組 比 傳進來的數組的 長度 還 多 1
int[] arr2 = new int[arr.length + 1];
//把傳進來的數組的 所有的元素 賦值給 新數組對應的位置
for (int i = 0; i < arr.length; i++) {
arr2[i] = arr[i];
}
//把新數組的最後一位 賦值爲 傳進來num
arr2[arr.length-1] = a;
return arr2;
}
}
- 寫一個方法sortMethod1,用於對int類型一維數組進行選擇排序;寫一個方法sortMethod2,用於對int類型一維數組進行冒泡排序。
package test;
public class SortMethod1 {
public static void main(String[] args) {
int[] arr = { 3, 4, 2, 5, 1 };
System.out.println("選擇排序的結果:");
sortMethod1(arr);
System.out.println();
System.out.println("--------------------");
System.out.println("冒泡排序的結果:");
sortMethod2(arr);
System.out.println();
}
public static void sortMethod1(int[] arr) {
// 選擇排序
int temp;
// 兩層循環 內層循環每次 得到一個最大值 外層循環 決定最大值要拿多少次
for (int i = 0; i < arr.length-1; i++) {
for (int j = i+1; j < arr.length; j++) {
//如果 你 j 比我 i 大 我就跟你換位
if (arr[j] > arr[i]) {
//交換兩個數
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
// 冒泡排序
public static void sortMethod2(int[] arr){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if(arr[j]>arr[j+1]){
int temp = arr[j+1];
arr[j+1]=arr[j];
arr[j] = temp;
}
}
}
for(int i = 0 ;i<arr.length;i++){
System.out.print(arr[i] +" ");
}
}
}
- 二維打地鼠
- 打包4個方法:歡迎的方法,遍歷地洞數組的方法,隨機出現敵人的方法,顯示遊戲結束統計的方法。 1. 打包4個方法:歡迎的方法,遍歷地洞數組的方法,隨機出現敵人的方法,顯示遊戲結束統計的方法。
- 初始化char類型的二維數組,每個洞都是 O
- 遊戲循環10次
- 地鼠隨機出現在二維數組中的某個元素位置,X
- 提示玩家擊打
- 判斷是否打中
- 擊中得2分,未擊中扣1分
- 遊戲結束,統計擊中次數,未擊中次數 和最後的得分。
- 按99重新開始(數據還原),按88退出遊戲
package test;
import java.util.Random;
import java.util.Scanner;
public class Home3 {
public static void main(String[] args) {
//定義變量
Scanner s = new Scanner(System.in);
int hit = 0;
int miss = 0;
int score = 0 ;
int count = 0 ;
//創建一個洞穴的二維數組 char
char[][] chs = new char[4][5];
welcome();
//初始化
init(chs);
System.out.println("洞穴初始化中、、、");
showHoles(chs);
//循環遊戲
while(count<10){
count++;
System.out.println("第"+count+"次,遊戲:");
//地鼠出現 隨機的行與列
int[] datas = ranEnemy(chs);
chs[ datas[0] ][ datas[1] ] = 'X';
System.out.println("請注意,地鼠出現了!!!");
showHoles(chs);
System.out.println("請輸入對應的行與列 進行 擊打:(用回車隔開) 99-重新開始 88-退出遊戲");
int pr = s.nextInt()-1;
if(pr==87){
System.out.println("謝謝再見!!!");
// break continue return
return;
}
if(pr== 98 ){
System.out.println("遊戲即將重新開始。。。");
// 分數 擊中 未擊中 次數 洞穴
score = 0;
hit = 0;
miss= 0;
count=0;
chs[ datas[0] ][ datas[1] ] ='O';
continue;
}
int pc = s.nextInt()-1;
//判斷
if( pr==datas[0] && pc == datas[1] ){
System.out.println("恭喜你打中了!");
hit++;
score+=2;
}else{
System.out.println("很遺憾,沒打中。。。");
miss++;
score--;
}
//還原
chs[ datas[0] ][ datas[1] ] ='O';
System.out.println("當前得分:"+ score);
}
//遊戲結束
over( hit,miss,score );
}
//初始化洞穴的方法 空格--》O
public static void init( char[][] arr ){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = 'O';
}
}
}
//歡迎
public static void welcome(){
System.out.println("*************");
System.out.println("歡迎來到打地鼠遊戲");
System.out.println("*************");
}
//char[][] 遍歷地洞的方法
public static void showHoles( char[][] arr ){
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
//隨機出現敵人 隨機 的 是兩個 角標 行int 列int
public static int[] ranEnemy(char[][] arr){
//隨機
Random r= new Random();
// 外層 --行 -- 二維數組的 長度
int row = r.nextInt(arr.length);
int col = r.nextInt(arr[row].length);// 剛纔那行的某一列
int[] datas = { row,col};
return datas;
}
//遊戲結束 a擊中 b未擊中 c 分數
public static void over( int a , int b, int c ){
System.out.println("**************");
System.out.println("game over");
System.out.println("擊中:"+ a);
System.out.println("未擊中:"+ b);
System.out.println("最終得分:["+ c+"]");
}
}