package Bibi.ArrayPack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
/**
* @Package: Bibi.ArrayPack
* @ClassName: ArrayTest
* @Date: 2020/12/1 16:10
* @Description: 数组
* 数组名、元素、下标、长度
* 特点:
* ①有序排列
* ②创建数组对象会在内存中开辟一整块连续的空间
* ③长度固定,不能修改
* ④属于引用数据类型,里面的元素可以是基本数据类型,也可以是引用数据类型
*
* 一维数组:
* 问题:
* ①一维数组的声明和初始化
* ②调用指定位置的元素
* ③获取数组长度
* ④遍历数组
* ⑤数组元素的默认初始化值
* >数组元素是整形:0
* >数组元素是浮点型:0.0
* >数组元素是char型:0或'\u0000'(对应的ascii码为0)
* >数组元素是boolean:false
* >数组元素是引用数据类型时:null
* ⑥数组的内存解析
* 二维数组:
* 同上分析
*
* 数据结构:
* 1.数据与数据之间的逻辑关系:集合、一对一、一对多、多对多
* 2.数据的存储结构:
* 线性表:顺序表(比如数组)、链表、栈、队列
* 树形结构:二叉树
* 图形结构:
*/
public class ArrayTest {
public static void func1() {
//1.一维数组的声明和初始化
int[] ids; //声明
//1.1静态初始化:数组的初始化和数组元素的赋值同时进行
ids = new int[]{1001, 1002, 1003};
//1.2动态初始化:数组的初始化和数组元素的赋值分开进行
String[] names = new String[2];
//总结:一旦初始化完成,长度也就确定了
//2.通过下标调用数组的指定位置的元素
names[0] = "小明";
names[1] = "小红";
String nameList = Arrays.toString(names);
System.out.println(nameList);
//3.数组的长度,属性length
System.out.println(names.length);
System.out.println(Arrays.toString(ids) + " 长度为:" + ids.length);
//4.遍历数组:for、for-each
//5.数组元素的初始化值
String[] arr = new String[3];
arr[1] = "小红";
for (String s : arr) {
System.out.println(s);
}
}
public static void func2() {
//1.二维数组的声明和初始化
//静态初始化
int[][] arr1 = new int[][]{{1, 2, 3}, {4, 5}, {6, 7, 8}};
int[][] arr11 = {{1, 2, 3}, {4, 5}, {6, 7, 8}};
//动态初始化
String[][] arr2 = new String[3][2];
String[][] arr3 = new String[3][];
for (int[] ints : arr1) {
System.out.println(Arrays.toString(ints));
}
//2.调用数组指定位置的元素
System.out.println(arr1[0][1]);
System.out.println(arr2[1][1]);
arr3[1] = new String[4];
System.out.println(arr3[1]); //[Ljava.lang.String;@10f87f48
System.out.println(Arrays.toString(arr3[1]));
//3.获取数组的长度
//4.遍历二维数组
for (int[] ints : arr11) {
for (int i : ints) {
System.out.print(i + " ");
}
System.out.println();
}
//5.数组元素的默认初始化值
int[][] arr4 = new int[4][3];
System.out.println(arr4[0]); //存放的是地址值:[I@312b1dae
System.out.println(arr4[0][0]);
}
public static void func3() {
System.out.println("请输入几层杨辉三角:");
Scanner scan = new Scanner(System.in);
int levels = scan.nextInt();
int[][] yangHui = new int[levels][];
for (int i = 0; i < levels; i++) {
int len = i + 1;
int[] cur = new int[len];
cur[0] = cur[len - 1] = 1;
for (int j = 1; j < i; j++) {
cur[j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j];
}
yangHui[i] = cur;
}
System.out.println(Arrays.toString(yangHui));
for (int[] ints : yangHui) {
System.out.println(Arrays.toString(ints));
}
}
public static void func4() {
//回形数
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[][] res = new int[n][n];
int[][] tmp = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int i = 0, j = 0, index = 0;
for (int num = 1; num <= n*n; num++) {
res[i][j] = num;
int next_i = i + tmp[index][0];
int next_j = j + tmp[index][1];
if (!(next_i >= 0 && next_i < n && next_j >= 0 && next_j < n && res[next_i][next_j] == 0)) {
index = (index + 1) % 4;
}
i = i + tmp[index][0];
j = j + tmp[index][1];
}
for (int[] ints : res) {
System.out.println(Arrays.toString(ints));
}
}
public static void main(String[] args) {
func2();
}
}
Java-数组[]
最新推荐文章于 2024-03-10 17:46:07 发布