JAVA基礎(第3天數組)

數組

  1. 先聲明
  2. 申請空間
  3. 賦值

一維數組

方式1int arr[ ] = { 1,2,3,4,5,6};
方式2int arr1[ ] = new int[ 6 ];
			arr[ 0 ] = 1;
			arr[ 1 ] = 2;
			arr[ 2 ] = 3;
			arr[ 3 ] = 4;
			arr[ 4 ] = 5;
			arr[ 5 ] = 6;
方式3int arr2[ ] = new int [ ]{ 1,2,3,4,5,6};

二維數組

	
//1聲明方式  中括號中不能指定個數
		int[][] arr1;
		int[] arr2[];
		int arr3[][];
		
//2給數組賦值(初始化)
		arr1 = new int[3][4];
		arr2 = new int[4][];//第二個括號可以不指定個數
		arr3 = new int[][4];//錯誤。
		
//給二維數組arr3申請空間(特殊情況)
		arr3 = new int[3][];
		arr3[0] = new int[5];
		arr3[1] = new int[6];
		arr3[2] = new int[4];
		//賦值
		arr1[0][0] = 34;
		arr3[0][0] = 3;
		arr3[0][1] = 4;
		arr3[0][2] = 5;
		//二維數組的遍歷
		//方法1
		for (int i = 0; i < arr3.length; i++) {
			for (int j = 0; j < arr3[i].length; j++) {
				System.out.print(arr3[i][j]+" ");
			}
			System.out.println();
		}
		System.out.println("************");
		//方法2:
		//遍歷二維數組中的每一個一維數組
		for (int[] arr : arr3) {
			//遍歷一維數組中的每一個元素
			for (int cell : arr) {
				System.out.print(cell+" ");
			}
			System.out.println();
		}

運行結果:
在這裏插入圖片描述


數組排序方法

//1Eclipse裏自帶排序的方法
Arrays.sort(arr[i]); 
Arrays.parallelSort(arr[i]);

int[] arr2 = {4,5,6,1,6,7,8,13,15};
//2將arr2的前n個元素賦給arr3
int[] arr3 = Arrays.copyOf(arr2, n);

//3選擇性的賦值(將arr2的第m個元素到n-1個元素賦給arr3)
//不包含to(第n個元素)位置
arr3 = Arrays.copyOfRange(arr2, m, n); 
System.out.println(Arrays.toString(arr3));

//4填充(如果arr3本來就有元素,填充後新數組元素代替了前面的元素)
int[] arr3 = {4,5,6,1,6,7,8,13,15};
Arrays.fill(arr3, 888);

//5將arr3的第m個元素到n-1個元素之間的元素被新賦值爲999;
Arrays.fill(arr3, m, n, 999);

//6判斷兩個數組的所有元素是否相等。返回值爲True和False;
Arrays.equals(arr5, arr6)
//7判斷字符串數組arr[i]的元素是否等於abc
"abc".equals(arr[i])

字符串拼接操作

//將字符串中的每個字符取出放入到一個字符數組中
 String str=“abcdefghjkl”;
 //toCharArray()方法將此字符串轉換爲一個新的字符數組。
char[] cs = str.toCharArray();
//定義一個字符串拼接的操作的類
StringBuffer sBuffer = new StringBuffer();
for(char c :cs) {//c爲每次循環的變量,cs爲每個字符
	char c2 = (char)(c^MY);
	sBuffer.append(c2);
}
number = input.nextInt();	
int newnumber = number%7;
6.	2
22
222
2222打印這種數字:代碼如下;  p=p*10+2;

7.	輸入一個字符串原碼轉密文,又轉原文的題型。
package question1;
import java.nio.charset.Charset;
public class Mingmiwenzhuanhuan_question1 {
	//key0是一種祕鑰
	private static final String key0 = "FECOI()*&<MNCXZPKL";
	private static final Charset charset = Charset.forName("UTF-8");
	//getBytes()方法使用指定的字符集將此 String 編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。
	private static byte[] keyBytes = key0.getBytes(charset);
 
	public static String encrypt(String enc) {
		byte[] b = enc.getBytes(charset);
		for (int i = 0, size = b.length; i < size; i++) {
			for (byte keyBytes0 : keyBytes) {
				b[i] = (byte) (b[i] ^ keyBytes0);
			}
		}
		return new String(b);
	}
	public static String decode(String dec) {
		byte[] e = dec.getBytes(charset);
		byte[] dee = e;
		for (int i = 0, size = e.length; i < size; i++) {
			for (byte keyBytes0 : keyBytes) {
				e[i] = (byte) (dee[i] ^ keyBytes0);
			}
		}
		return new String(e);
	}
	public static void main(String[] args) {
		String s = "you are right";
		String enc = encrypt(s);
		String dec = decode(enc);
		System.out.println("加密後:" + enc);
		System.out.println("解密後:" + dec);
	}
 
}
8.一個年份有多少天
package lianxi1;

import java.util.Scanner;
public class Calc {
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		boolean ist = true;
		while (ist) {
			System.out.println("請輸入一個年份");
			int year = input.nextInt();
			System.out.println("請輸入一年中的十二個月份中的任意一個月份!");
			int month = input.nextInt();
			while (month <= 0 || month > 12) {
				System.out.println("你輸入的月份有誤,請重新輸入!");
				month = input.nextInt();
			}
			int day=0 ;
			boolean isTrue = true;
			while (isTrue) {
				System.out.println("請輸入一個日子");
				day = input.nextInt();
				if (month == 2 && month % 4 == 0 && month % 100 != 0) {
					if (day <= 0 || day > 29) {
						System.out.println("錯誤1:你輸入月份對應的天數有誤,請重新輸入!");
						day = input.nextInt();
					}else {
						break;
					}
				}
				 if (month == 2 && month % 4 != 0 && month % 100 == 0) {
					if (day <= 0 || day > 28) {
						System.out.println("錯誤2:你輸入月份對應的天數有誤,請重新輸入!");
						day = input.nextInt();
					}else {
						break;
					}
				}
				if (month == 1 || month == 3 || month == 5 || month == 3 || month == 7 || month == 8 || month == 10
						|| month == 12) {
					if (day < 0 || day > 31) {
						System.out.println("錯誤3:你輸入月份對應的天數有誤,請重新輸入!");
						day = input.nextInt();
					}else {
						break;
					}
				}
				if (month == 4 || month == 6 || month == 9 || month == 11) {
					
					if (day <= 0 || day > 30) {
						System.out.println("錯誤4:你輸入月份對應的天數有誤,請重新輸入!");
						day = input.nextInt();
					}else {
						break;
					}
				}
				isTrue = false;
			}
			int sumday = 0;
			switch (month - 1) {
			// 1 3 5 7 8 10 12 爲大月
			// 4 6 9 11 爲小月
			case 12:
				sumday += day;
			case 11:
				sumday += 30;
			case 10:
				sumday += 31;
			case 9:
				sumday += 30;
			case 8:
				sumday += 31;
			case 7:
				sumday += 31;
			case 6:
				sumday += 30;
			case 5:
				sumday += 31;
			case 4:
				sumday += 30;
			case 3:
				sumday += 31;
			case 2:
				sumday += 28;
			case 1:
				sumday += 31;
			}
			if (month % 4 == 0 && month % 100 != 0) {
				System.out.println(month + "月"+day+"日"+ "是一年的第" + (sumday + day) + "天");
			}else if(month<2) {
				System.out.println(month + "月"+day+"日"+ "是一年的第" + (sumday + day) + "天");
			}
			else {
				System.out.println(month + "月"+day+"日"+ "是一年的第" + (sumday + day + 1) + "天");
			}
		}
	}
}


9.1 9*9乘法表
package progect1_2;

public class Multiply1 {
	public static void main(String[] args) {
		for(int i=1;i<=9;i++) {
			for(int j = 1;j<=i;j++) {
				System.out.print(i+"*"+j+"= "+(i*j)+"\t\t");
			}
			System.out.println();
		}
	}
}
9.2 9*9乘法表
package progect1_2;

public class Multiply2 {

	public static void main(String args[]) {

		for (int i = 1, j = 1; i <= 9; j++) {
			System.out.print( i + "*" + j + "=" + i * j+"\t");
			if (i == j) {
				//改變j的循環條件
				j = 0;
				i++;
				System.out.println();
			}
		}
	}
}
打印楊輝三角
package progect1_1;

public class Yanghui {
	public static void main(String[] args) {
		int arr[][] = new int[7][];
		for (int i = 0; i < arr.length; i++) {//控制行數
			arr[i] = new int [i+1];
			for (int j = 0; j < arr[i].length; j++) {//控制列數
				if (j == 0 || j == i ) {
					arr[i][j] = 1;
				}else {
					arr[i][j] = arr[i-1][j]+arr[i-1][j-1];
				}
				System.out.print(arr[i][j]+" ");
			}
			System.out.println();
		}
	}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章