上一次数组感觉有个重点没写上当数组相等 当:定义`arr1 = arr2;`
此时两数组完全相等因为传输了首地址相等(指针)
这一波主要也就是锻炼思维,后面Arrays工具直接解决
数组基本操作
求和、平均数、找最大值、找最小值
package com.lys.demo2;
import java.util.Scanner;
public class AlgorithmArray {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array: ");
int n = sc.nextInt();
int[] arr1 = new int[n];
System.out.println("Enter the elements of array: ");
for (int i = 0; i < n; i++) {
arr1[i]=sc.nextInt();//输入数组
}
//GetMax
int max=arr1[0];
for (int i = 1; i < n; i++) {
if (max<arr1[i]) {
max=arr1[i];
}
}
System.out.println("The maximum value is "+max);
//GetMin
int min=arr1[0];
for (int i = 1; i < n; i++) {
if (min>arr1[i]) {
min=arr1[i];
}
}
System.out.println("The minimum value is "+min);
//GetSum
int sum=0;
for (int i = 0; i < n; i++) {
sum+=arr1[i];
}
System.out.println("The sum of all elements is "+sum);
//GetAverage
double average=sum/n;
System.out.println("The average of all elements is "+average);
}
}
数组反转的两种方法
其实细看也差不多(其实就是一摸一样,格式有点不同)
package com.lys.demo2;
//数组反转
public class ArrayReversalTest {
public static void main(String[] args) {
int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
System.out.println();
int n = 0,m = arr.length-1;
for(int i = 0 ; i < arr.length/2 ; i++){
int temp = arr[n];
arr[n] = arr[m];
arr[m] = temp;
n++;
m--;
}
for(int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
//方法二
for(int i = 0,j = arr.length-1 ; i < arr.length/2 ; i++,j--){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for(int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i]+" ");
}
}
}
遍历数组
输出所有扑克牌(最后一步for只是为了美观)
package com.lys.demo2;
public class PlayingCard {
public static void main(String[] args) {
String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
String[] ranks = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
String[] indexs = new String[(suits.length * ranks.length)+2];
indexs[indexs.length-2] = "big jocker ";
indexs[indexs.length-1] = "small jocker";
int n = 0;
for (int i = 0; i < suits.length; i++){
for (int j = 0; j < ranks.length; j++){
indexs[n++] = suits[i] + ranks[j];
}
}
for (int i = 0; i < indexs.length; i++){
if ((i)%13==0){
System.out.println();
}
System.out.print(indexs[i]);
}
}
}
数组查找(可以去看黑马的c语言,每一个方法都是一个视频)
1.遍历查找
(函数的使用方法还真被我猜对了,和c也差不多)
package com.lys.demo2;
import java.util.Scanner;
public class LinearSearch {
public static int linearSearch(int[] arr, int key) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[]{1,2,3,4,5,6,7,8,9};
System.out.println("请输入你想要查找的数字");
int key = sc.nextInt();
int result = linearSearch(arr, key);
System.out.println("这个数字在这个数组中的索引是" + result);
}
}
2.二分查找
package com.lys.demo2;
import java.util.Scanner;
public class BinarySearch {
public static int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Scanner sc = new Scanner(System.in);
int target = sc.nextInt();
System.out.println(binarySearch(arr, target));
}
}
数组排序
1.冒泡排序
package com.lys.demo2;
public class BubbleSort {
public static void bubbleSort(int[] arr){
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = new int[]{4,7,3,5,2,6,1,9};
bubbleSort(arr);
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
2.选择排序(更好理解,难度也不大)
package com.lys.demo2;
public class SelectSort {
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = new int[]{7,3,5,2,6,1,8,4};
selectSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
ok也是开始面向对象了 争取在期中之前学完javase 直接冲