1、数组
- 程序=算法+数据结构
- 算法:解决程序的流程步骤(顺序、分支、循环)
- 数据结构:将数据按照某种特定的结构来存储
- 设计良好的数据结构会导致良好的算法
ArrayList、LinkedList
- 存放整数类型的数组
- 长度是4
- 只能存放int类型
- 元素类型[] 数组名 = new 元素类型[长度];
int[] array1 = new int[3];
double[] array2 = new double[3];
boolean[] array3 = new boolean[3];
char[] array4 = new char[3];
// Student[] array5 = new Student[3];
- 在定义的时候直接赋值 : int[] array = {32,5,7,87}; int[] array = new int[]{32,5,7,87};
- 先定义后赋值 : int[] array = new int[4]; array[0] = 34;
冒泡排序
public class Code02_BubbleSort {
public static void bubbleSort(int[] arr) {
if (arr == null || arr.length < 2) {
return;
}
for (int e = arr.length - 1; e > 0; e--) {
for (int i = 0; i < e; i++) {
if (arr[i] > arr[i+1]) {
swap(arr, i, i+1);
}
}
}
}
public static void swap(int[] arr, int i, int j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
public static void comparator(int[] arr) {
Arrays.sort(arr);
}
public static int[] generateRandomArray(int maxSize, int maxValue) {
int[] arr = new int[(int) ((maxSize+1) * Math.random())];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) ((maxValue + 1) * Math.random()) - ((int) (maxValue * Math.random()));
}
return arr;
}
public static int[] copyArray(int[] arr) {
if (arr == null) {
return null;
}
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
public static boolean isEqual(int[] arr1, int[] arr2) {
if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
return false;
}
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1.length != arr2.length) {
return false;
}
for (int i = 0; i< arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
public static void printArray(int[] arr) {
if (arr == null) {
return;
}
for (int i =0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
int testTime = 500000;
int maxSize = 100;
int maxValue = 100;
boolean success = true;
for (int i = 0; i < testTime; i++) {
int[] arr1 = generateRandomArray(maxSize,maxValue);
int[] arr2 = copyArray(arr1);
bubbleSort(arr1);
comparator(arr2);
if (!isEqual(arr1, arr2)) {
success = false;
printArray(arr1);
printArray(arr2);
break;
}
}
System.out.println(success ? "Nice!" : "Fucking fucked!");
int[] arr = generateRandomArray(maxSize,maxValue);
printArray(arr);
bubbleSort(arr);
printArray(arr);
}
}
练习:
- 1、int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9}; 求出上面数组中0-9分别出现的次数
public void test02() {
int[] scores = { 0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9 };
int[] sum = new int[10];
for (int i = 0; i < sum.length; i++) {
for (int j = 0; j < scores.length; j++) {
if (scores[j] == i) {
sum[i]++;
}
}
System.out.print(sum[i] + " ");
}
}
public void test05() {
int[] scores = { 0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9 };
int[] sum = new int[10];
for (int i = 0; i < scores.length; i++) {
int num = scores[i];
sum[num]++;
}
for (int i = 0; i < sum.length; i++) {
System.out.println(i + "出现了" + sum[i] + "次");
}
}
- 2、 int[] scores={0,0,1,2,3,5,4,5,2,8,7,6,9,5,4,8,3,1,0,2,4,8,7,9,5,2,1,2,3,9}; 要求求出其中的奇数个数和偶数个数。
public void test03() {
int[] scores = { 0, 0, 1, 2, 3, 5, 4, 5, 2, 8, 7, 6, 9, 5, 4, 8, 3, 1, 0, 2, 4, 8, 7, 9, 5, 2, 1, 2, 3, 9 };
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < scores.length; i++) {
if (scores[i] % 2 != 0) {
sum1++;
} else {
sum2++;
}
}
System.out.println("奇数个数:" + sum1);
System.out.println("偶数个数:" + sum2);
}
- 3、 输入一组学生的成绩,使用数组,然后计算他们的平均值, int[] scores = new int[3];
public void test04() {
Scanner scanner = new Scanner(System.in);
int[] arr = new int[3];
int sum = 0;
System.out.print("请输入成绩:");
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
if (arr[i] < 0 || arr[i] > 100) {
System.out.println("输入成绩非法!");
return;
}
sum += arr[i];
}
System.out.println("平均成绩为:" + (sum * 1.0) / arr.length);
}
- 4、 一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
public void test06() {
char[] arr = { 'a', 'b', 'c', 'b', 'a' };
boolean isPalindrome = true;
for (int i = 0, j = arr.length - 1; i < j; ) {
if (arr[i] == arr[j]) {
i++;
j--;
} else {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
System.out.println("数组arr是回文数组");
} else {
System.out.println("数组arr不是回文数组");
}
}
- 5、 输入一行字符串,分别统计出其中英文字母、空格、数字和其它字符的个数。
public void test07() {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
System.out.println(str);
char[] arr = str.toCharArray();
int sumSpace = 0;
int sumNum = 0;
int sumLetter = 0;
int sumOther = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= '0' && arr[i] <= '9') {
sumNum++;
} else if ((arr[i] >= 65 && arr[i] <= 90) || (arr[i] >= 97 && arr[i] <= 122)) {
sumLetter++;
} else if (arr[i] == ' ') {
sumSpace++;
} else {
sumOther++;
}
}
System.out.println("数字的个数为:" + sumNum);
System.out.println("字母的个数为:" + sumLetter);
System.out.println("空格的个数为:" + sumSpace);
System.out.println("其他字符的个数为:" + sumOther);
}
|