目录
数组的概述和静态初始化
package 数组;
public class array1 {
public static void main(String[] args){
//格式
//静态初始化
//数据类型 [] 数组名 = new 数组类型[]{元素1,元素2}
//姓名
String [] name1 = new String[]{"12","13","14"};
String [] name2 = {"12","13","14"};
//年龄
int [] age1 = new int[]{20,21,22};
int [] age2 = {20,21,22};
//身高
double [] cm1 = new double[]{1.78,1.55,1.20};
double [] cm2 = {1.78,1.55,1.20};
}
}
数组的地址值和元素访问
package 数组;
public class array2 {
public static void main(String[] args){
//格式
//静态初始化
//数据类型 [] 数组名 = new 数组类型[]{元素1,元素2}
//姓名
String [] name1 = new String[]{"12","13","14"};
String [] name2 = {"12","13","14"};
//年龄
int [] age1 = new int[]{20,21,22};
int [] age2 = {20,21,22};
//身高
double [] cm1 = new double[]{1.78,1.55,1.20};
double [] cm2 = {1.78,1.55,1.20};
String number1 = name1[0];
int number2 = age1[0];
double number3 = cm1[0];
System.out.println("我叫" + number1 + "!");
System.out.println("今年" + number2 + "岁!");
System.out.println("我的身高是" + number3 + "m!");
System.out.println(name1[1]);
age1[0] = 121;
System.out.println(age1[0]);
}
}
数组的遍历
package 数组;
import sun.security.util.Length;
public class arraylx1 {
public static void main(String[] args){
/* 定义一个数组,存储1,2,3,4,5,6,7,8,9,10遍历数组得到每一个元素。
要求:
1,如果是奇数,则将当前数字扩大两倍
2,如果是偶数,则将当前数字变成二分之一
*/
int number[] = {1,2,3,4,5,6,7,8,9,10};
for (int i =0 ; i < number.length ; i++){
if (number[i] % 2 == 0){
number[i] = number[i] / 2;
}else{
number[i] = number[i] * 2;
}
}
for (int i = 0 ; i < number.length; i++){
System.out.println(number[i]);
}
}
}
数组的动态初始化
package 数组;
public class array3 {
public static void main(String[] args){
int[] number = new int[50];
number[0] = 1;
number[1] = 2;
number[2] = 3;
System.out.println(number[0]);
System.out.println(number[1]);
System.out.println(number[2]);
System.out.println(number[3]);
}
}
//数组默认初始化值的规律
//整数类型:默认初始化值0
//小数类型:默认初始化值0.0
//字符类型:默认初始化值 \ueeee’ 空格
//布尔类型:默认初始化值 false
//引用数据类型:默认初始化值 null
数组练习
练习
package 数组;
import javafx.geometry.Bounds;
import javax.swing.*;
import java.util.Random;
public class arraylx2 {
public static void main(String[] args) {
/*需求:生成10个1~10之间的随机数存入数组
1) 求出所有数据的和
2)求所有数据的平均数
3) 统计有多少个数据比平均值小*/
//定义数组
int[] arr = new int[10];
//把随机数存入数组
Random r = new Random();
for(int i = 0;i < arr.length;i++){
int number = r.nextInt(100) + 1;
arr[i] = number;
}
//1) 求出所有数据的和
int num = 0;
for (int i = 0; i < arr.length; i++) {
num = num + arr[i];
}
System.out.println("所有数的和是" + num);
//2)求所有数据的平均数
int veg = 0;
for (int i = 0; i < arr.length; i++) {
veg = num / arr.length;
}
System.out.println("所有数据的平均数为" + veg);
//3) 统计有多少个数据比平均值小
int conent = 0;
for (int i = 0; i < arr.length; i++) {
if(arr[i] < veg){
conent = conent + 1;//conent++
}
}
System.out.println("有" + conent + "个数据比平均值小");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
package 数组;
public class arraylx3 {
public static void main(String[] args) {
/*需求: 定义一个数组,存入1,2,3,4,5。
交换首尾索引对应的元素.交换前: 1,2,3,4,5,交换后: 5,2,3,4,1*/
//定义数组存储数据
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + " ");
}
}
}