文章目录
一、数组定义
1.1 静态数组
数组定义、访问、取长度
package com.ith.create;
public class ararrydemo1 {
public static void main(String[] args) {
//定义
double[] data = new double[]{99.5,65,35,87,23,78,98};
int[] age = new int[]{1,2,3,4,5,6,7};
String[] names = {"张三","李四","王二"};
//访问
for (int i=0;i<3;i++){
System.out.println(names[i]+"的分数是"+data[i]);
}
//赋值
data[2] = 100;
System.out.println(data[2]);
//取数组长度
System.out.println("data长度:"+data.length);
System.out.println("data最大索引:"+(data.length-1));
}
}
1.2 动态数组
package com.ith.create;
public class arraydemo4 {
public static void main(String[] args) {
//动态数组
double[] scores = new double[3];
scores[0] = 99.8;
System.out.println(scores[0]);
System.out.println(scores[2]);
String[] name = new String[90];
name[0]="zhangsan";
name[2]="lisi";
System.out.println(name[0]);
System.out.println(name[1]);
System.out.println(name[2]);
}
}
1.3 元素默认值
package com.ith.create;
public class arrarydemo5 {
public static void main(String[] args) {
//默认值
//整形
int[] arr = new int[10];
System.out.println(arr[0]);
System.out.println(arr[9]);
//字符型
char[] chars = new char[100];
System.out.println((int)chars[0]);
System.out.println((int)chars[99]);
//浮点型
double[] s = new double[10];
System.out.println(s[0]);//0.0
System.out.println(s[9]);
//布尔型
boolean[] booleans = new boolean[10];
System.out.println(booleans[0]);
System.out.println(booleans[9]);//false
//引用类型
String[] names = new String[10];
System.out.println(names[0]);
System.out.println(names[9]);//null
}
}
二、数组遍历
2.1 基础
package com.ith.traverse;
public class arraydemo0 {
public static void main(String[] args) {
int[] arr = {12,23,34,45,56};
//arr.length 数组长度
for(int i=0;i<arr.length;i++){
System.out.println(arr[i]);
}
}
}
2.2 数组求和
数组求和
package com.ith.demo;
public class test1 {
public static void main(String[] args) {
int[] money = {12,45,67,32,76};
int sum=0;
for(int i=0;i<money.length;i++){
sum +=money[i];
}
System.out.println("数组元素和为:"+sum);
}
}
三、数组案例
3.1 数组最大值
package com.ith.demo;
public class test2 {
public static void main(String[] args) {
//最大值
int[] scores = {12,43,76,989,423,56,87};
int max = scores[0];
for (int i = 0; i < scores.length; i++) {
if(scores[i]>max){
max = scores[i];
}
}
System.out.println(max);
}
}
3.2 猜数字
package com.ith.demo;
import java.util.Random;
import java.util.Scanner;
public class test3 {
public static void main(String[] args) {
//产生1-20随机数
//动态数组
int[] data = new int[5];
Random r=new Random();
for (int i = 0; i < data.length; i++) {
data[i] = r.nextInt(20)+1;
}
Scanner sc = new Scanner(System.in);
OUT:
while(true){
System.out.println("请输入:");
int guess = sc.nextInt();
for (int i = 0; i < data.length; i++) {
if(data[i] == guess){
System.out.println("猜中了,数组索引是:" + i);
break OUT;//结束整个循环
}
}
System.out.println("一个都没猜中,继续猜测:");
}
System.out.print("数组的全部元素是:");
for (int i = 0; i < data.length; i++) {
System.out.print(data[i]+"\t");
}
}
}
3.3 随机排名
package com.ith.demo;
import java.util.Random;
import java.util.Scanner;
public class test4 {
public static void main(String[] args) {
//随机排名
int[] codes = new int[5];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < codes.length; i++) {
System.out.println("请输入第" + (i + 1) + "个员工的工号:");
int code = sc.nextInt();
codes[i] = code;
}
// 12 35 36 37 38
Random r = new Random();
for (int i = 0; i < codes.length; i++) {
//随机索引
int index = r.nextInt(codes.length);
int temp = codes[index];
codes[index] = codes[i];
codes[i] = temp;
}
for (int i = 0; i < codes.length; i++) {
System.out.print(codes[i] + "\t");
}
}
}
可以用于洗牌
3.4 数组排序
冒泡排序
package com.ith.demo;
public class test5 {
public static void main(String[] args) {
int[] arr = {5,4,3,2};
for (int i = 1; i <= arr.length-1; i++) {
for(int j=0;j<arr.length-i;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] + "\t");
}//2 3 4 5
}
}
四、数组内存
package com.ith.memory;
public class demo1 {
public static void main(String[] args) {
int a = 12;
System.out.println(a);
int[] arr = {11,22,33};
System.out.println(arr);
arr[0] = 44;
arr[1] = 55;
arr[2] = 66;
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
}
}
package com.ith.memory;
public class demo1 {
public static void main(String[] args) {
int a = 12;
System.out.println(a);
int[] arr = {11,22,33};
System.out.println(arr);
arr[0] = 44;
arr[1] = 55;
arr[2] = 66;
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
}
}
/*
*输出:
[I@16b98e56
[I@16b98e56
99
99
* */
两种异常
package com.ith.attention;
public class test {
public static void main(String[] args) {
int[] arr = {11,22,33};
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
//System.out.println(arr[3]);//出现异常
arr = null;
System.out.println(arr);
//System.out.println(arr.length);//异常
System.out.println("程序结束。。。");
}
}