day06
1.数组复制,遍历,排序,查找
import java.util.Arrays;
public class ArraysTest {
public static void main(String[] args) {
int[] arr1 = {9, 6, 3, 8, 5};
int[] arr2 = Arrays.copyOf(arr1, 5);
int[] arr3 = Arrays.copyOfRange(arr1, 3, 6);
//用toString代替for循环
System.out.println(Arrays.toString(arr2));
Arrays.sort(arr2);//排序
System.out.println(Arrays.toString(arr2));
int a = Arrays.binarySearch(arr2,5);//查询
System.out.println(a);
System.out.println(Arrays.toString(arr3));
Arrays.fill(arr3, 10);
System.out.println(Arrays.toString(arr3));
}
}
2.//实现一个方法,参数是int[] //该方法可以计算出数组中所有数据的平均值,并且把结果返回
public class ArraysTest2 {
//实现一个方法,参数是int[]
//该方法可以计算出数组中所有数据的平均值,并且把结果返回
public double avg(int[] a) {
double sum = 0;
for (int i = 0; i < a.length; i++) {
sum+=a[i];
}
return sum/a.length;
}
public int max(int[] a) {
int max = 0;
for (int i = 0; i < a.length; i++) {
/*if (max<a[i]) {
max = a[i];
}*/
max = (max<a[i])?a[i]:max;
}
return max;
}
public static void main(String[] args) {
ArraysTest2 arraysTest2 = new ArraysTest2();
System.out.println(arraysTest2.avg(new int[] {1,5,6,4,3}));
int[] a = {1,2,3,4};
System.out.println(arraysTest2.max(a));
}
}
3.插入排序
import java.util.Arrays;
public class ChaRu {
public void test(int[] a) {
int currentValue;
int pos;
//6 9 6 8 5 3 7 1
for (int i = 1; i < a.length; i++) {
currentValue = a[i];//9
pos = i;//1
for (int j = i-1; j >= 0; j--) {
if (a[j] > currentValue) {
a[j+1] = a[j];
pos = j;
}else {
break;
}
}
if (pos!=i) {
a[pos] = currentValue;
}
}
}
public static void main(String[] args) {
ChaRu chaRu = new ChaRu();
int[] a = {6,9,6,8,5,3,7,1};
chaRu.test(a);
System.out.println(Arrays.toString(a));
}
}
4.生成随机数进行查找
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class ChaZhao {
public static void main(String[] args) {
//随机生成数组
Random r = new Random();
int[] arr = new int[20];
for (int i = 0; i < arr.length; i++) {
arr[i] = r.nextInt(100);
}
System.out.println(Arrays.toString(arr));
//定义查找的返回值,如果找到,将循环的值赋值给它,没找到,就是原本定义的-1。
int index = -1;
//随机输入数
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 0; i < arr.length; i++) {
if (n == arr[i]) {
//如果找到了,记录索引位置
index = i;
if (index >= 0) {
System.out.println("输入的值" + n + "位于该数组的第" + (index + 1) + "个位置");
}
}
}
if (index <0) {
System.out.println("输入的值" + n + "没有找到"); }
}
}
5.冒泡排序
import java.util.Arrays;
public class Maopao {
//【冒泡排序】
public void test(int[] a) {
//轮数
for (int i = 0; i < a.length-1; i++) {
//次数
for (int j = 0; j < a.length-i-1; j++) {
if (a[j]>a[j+1]) {
a[j] = a[j]^a[j+1];
a[j+1] = a[j]^a[j+1];
a[j] = a[j]^a[j+1];
}
}
}
}
public static void main(String[] args) {
int[] array = {7,4,9,10,6,7,6};
Maopao maopao = new Maopao();
maopao.test(array);
System.out.println(Arrays.toString(array));
}
}
6.随机数和出现频率
public class SuiJiShu {
public static void main(String[] args) {
int[] randArray = new int[100];
int[] countArray = new int[6];
int randValue;
for (int i = 0; i < 100; i++) {
randValue = (int) (Math.random() * 6 + 1);
randArray[i] = randValue;
System.out.print(randValue);
if ((i + 1) % 10 == 0)
System.out.println();
countArray[randArray[i] - 1]++;
}
System.out.println();
for (int i = 0; i < 6; i++) {
System.out.println("数字 " + (i + 1) + " 出现频率: " + countArray[i] + "%");
}
}
}
7.选择排序
public class Xuanze {
//选择排序
public void test(int[] a) {
int index_should = 0;
int index_now = 0;
//轮数:长度-1
for (int i = 0; i < a.length - 1; i++) {
//去找最小值
index_should = i;
index_now = i;
for(int j = i+1;j < a.length;j++) {
if (a[j]<a[index_now]) {
index_now = j;
}
}
if (a[index_now]!=a[index_should]) {
a[index_now] = a[index_now]^a[index_should];
a[index_should] = a[index_now]^a[index_should];
a[index_now] = a[index_now]^a[index_should];
}
}
}
public static void main(String[] args) {
int[] array = {1,5,9,7,6,2};
Xuanze xuanze = new Xuanze();
xuanze.test(array);
System.out.println(Arrays.toString(array));
}
}
8.生成4位的随机验证码
public class YanZhengMa {
String value = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
char[] data;
public void init(){
data = value.toCharArray();
}
public String getRandString(){
char[] randChars = new char[4];
int randIndex;
for(int i = 0;i < randChars.length;i++){
randIndex = (int)(Math.random()*data.length);
randChars[i] = data[randIndex];
}
return new String(randChars);
}
public static void main(String[] args){
YanZhengMa yanZhengMa = new YanZhengMa() ;
yanZhengMa.init();
String randString = yanZhengMa.getRandString();
System.out.println("验证码:" + randString);
}
}