public class Test{
public static void main(String[] args) {
//1.定义数组
//1.1 声明数组
String[] names;
int scores[];
//1.2 初始化
//1.2.1 静态:初始化数组与元素赋值同时进行
names = new String[]{"一","二","三"};
//1.2.2 动态:初始化数组与元素赋值分开
scores = new int[4];
scores[0] = 1;
scores[1] = 2;
scores[2] = 3;
scores[3] = 4;
//2.元素调用(通过数组元素下标进行调用,从0开始,n-1结束,n为数组长度)
System.out.println(names[0]);
System.out.println(scores[0]);
//3.数组长度:length属性获取数组长度
System.out.println(names.length);
System.out.println(scores.length);
//4.数组遍历
for(int i = 0;i < names.length;i++){
System.out.println(names[i]);
}
}
}
初始化数组后,未赋值
( 基本数据类型:
byte,short,int,long默认为0
;
float,double默认0.0
char默认空格
boolean默认false
引用数据类型:
默认值为null
)
public class Test1{
public static void main(String[] args) {
int[] names = new int[4];
names[0] = 11;
names[1] = 22;
names[3] = 44;
for(int i = 0;i < names.length;i++){
System.out.println(names[i]);
}
float[] names1 = new float[4];
names1[0] = 1.1F;
names1[1] = 2.2F;
names1[3] = 4.4F;
for(int i = 0;i < names1.length;i++){
System.out.println(names1[i]);
}
}
}
数组一旦初始化,其长度是不变的
public class Test{
public static void main(String[] args) {
int[] i = new int[]{11,221,2112,121};
//此时i是不能改变的
int[] j = new int[10];
for(int k = 0;k < i.length;k++){
j[k] = i[k];
}
j[9] = 101;
j[8] = 111;
for(int k = 0;k < j.length;k++){
System.out.println(j[k]);
}
}
}
class Printimive{
boolean[] t = new boolean[3];
}
public class TestPrintimive{
public static void main(String[] args){
Printimive d = new Printimive();
for(int i = 0;i < d.t.length;i++){
System.out.println(d.t[i]);
}
for(int i = 0;i < d.t.length;i++){
d.t[i] = true;
System.out.println(d.t[i]);
}
}
}
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("请输入学生人数:");
int d = s.nextInt();
int[] scores = new int[d];
int max = 0;
for(int i = 0;i < scores.length;i++){
System.out.println("请输入第" + (i+1) + "名学生的成绩:");
int score = s.nextInt();
scores[i] = score;
if(scores[i] > max){
max = scores[i];
}
}
System.out.println("最高分是:" + max);
for(int i = 0;i < scores.length;i++){
char level;
if(scores[i] >= max -10){
level = 'A';
}else if(scores[i] >= max -20){
level = 'B';
}else if(scores[i] >= max -30){
level = 'C';
}else{
level = 'D';
}
System.out.println("第" + (i+1) + "名学生成绩等级为:" + level);
}
}
}
public class Test{
public static void main(String[] args){
//二维数组声明
int[][] scores1;
String[][] scores2;
//静态初始化
scores1 = new int[][]{{1,2,3},{3,4,5},{6}};
//动态初始化
scores2 = new String[6][6];
scores2 = new String[4][];
scores2[0] = new String[8];
scores2[1] = new String[88];
scores2[2] = new String[888];
//2.引用具体的某一个元素
int[][] i = new int[3][2];
i[1][0] = 9;
i[2][1] = 99;
//3.二维数组长度
System.out.println(i.length); //i数组的长度//3
System.out.println(i[0].length); //i数组中某个元素的长度
//4.二维数组遍历
for(int m = 0;m < i.length;m++){
for(int k = 0;k < i[m].length;k++){
System.out.print(i[m][k] + " ");
}
System.out.println();
}
}
}
特殊写法:int[]x,y[];x为一维数组,y为二维数组。
1.数组下标越界:java.lang.ArrayIndexOutOfBoundsException
int[] i = new int[10];
i[0] = 99;
i[10] = 9;
2.空指针:java.lang.NullPointerException
boolean[] b = new boolean[3];
b = null;
System.out.println(b[0]);
public class Test{
public static void main(String[] args){
int[][] arr = new int[][]{{3,8,2},{2,7},{9,0,1,6}};
int sum = 0;
for(int i = 0;i < arr.length;i++){
for(int j = 0;j < arr[i].length;j++){
System.out.print(arr[i][j] + "\t");
sum += arr[i][j];
}
System.out.println();
}
System.out.println("总和为:" + sum);
}
}
public class Test{
public static void main(String[] args){
int[][] m = new int[10][];
//初始化
for(int i = 0;i < m.length;i++){
m[i] = new int[i + 1];
}
//赋值
for(int j = 0;j < m.length;j++){
for(int k = 0;k < m[j].length;k++){
m[j][0] = m[j][j] = 1;
if(j > 1 && k >0 && k < j){
m[j][k] = m[j-1][k] + m[j-1][k-1];
}
}
}
//遍历
for(int i = 0;i < m.length;i++){
for(int j = 0;j < m[i].length;j++){
System.out.print(m[i][j] + "\t");
}
System.out.println();
}
}
}
public class Test{
public static void main(String[] args){
int[] m = new int[]{1,2,32,22,-12,26,99};
//最大值
int max = m[0];
for(int i = 1;i < m.length;i++){
if(max < m[i]){
max = m[i];
}
}
System.out.println("数组最大值为:" + max);
//最小值
int min = m[0];
for(int i = 1;i < m.length;i++){
if(min > m[i]){
min = m[i];
}
}
System.out.println("数组最小值为:" + min);
//总和
int sum = 0;
for(int i = 0;i < m.length;i++){
sum += m[i];
}
System.out.println("数组总和为:" + sum);
//平均
float avg = (float)sum/m.length;
System.out.println("数组平均值为:" + avg);
//数组的复制(
![在这里插入图片描述](https://img-blog.csdnimg.cn/cf0e56a281a84277a93ad1de1fbe4477.png)
//m1 = m;此时m1复制的是m的内存地址,并不是复制的m的元素
int[] m1 = new int[m.length];
for(int i = 0;i < m1.length;i++){
m1[i] = m[i];
}
//数组元素的反转
for(int i = 0;i < m1.length;i++){
int temp = m1[i];
m1[i] = m1[m1.length - 1 - i];
m1[m1.length - 1 - i] = temp;
}
//遍历
for(int i = 0;i < m1.length;i++){
System.out.print(m1[i] + "\t");
}
//冒泡排序
//从小到大
for(int i = 0;i < m1.length - 1;i++){
int t = i;//默认i处是最小值
for(int j = i;j < m1.length;j++){
if(m1[t] > m1[j]){
t = j;
}
}
if(t != i){
int temps = m1[t];
m1[t] = m1[i];
m1[i] = temps;
}
}
System.out.println();
//遍历
for(int i = 0;i < m1.length;i++){
System.out.print(m1[i] + "\t");
}
}
}
感谢大家的支持,关注,评论,点赞!
参考资料:
尚硅谷宋红康20天搞定Java基础上部
69