Java零基础入门 8:Java数组
Posted 哪 吒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java零基础入门 8:Java数组相关的知识,希望对你有一定的参考价值。
🍅 Java学习路线配套文章:搬砖工逆袭Java架构师
🍅 Java经典面试题大全:10万字208道Java经典面试题总结(附答案)
🍅 简介:Java领域优质创作者🏆、CSDN哪吒公众号作者✌ 、Java架构师奋斗者💪
🍅 扫描主页左侧二维码,加入群聊,一起学习、一起进步
🍅 欢迎点赞 👍 收藏 ⭐留言 📝
目录
一、数组
数组是一种数据结构,是用来存储同一类型值的集合。通过一个整形下标可以访问数组中的每一个值。
private static void test04(){
String[] arr = new String[]{"哪吒","云韵","比比东","美杜莎"};
for (int i = 0; i<arr.length;i++) {
System.out.println(arr[i]);
}
}
二、数组拷贝
1、拷贝数值类型
package com.nezha.javase;
import java.util.Arrays;
public class ArrayTest {
public static void main(String[] args) {
String[] arr = new String[]{"哪吒","云韵","比比东","美杜莎"};
String[] arr2 = new String[4];
for (int i = 0; i < arr.length; i++) {
arr2[i] = arr[i];
}
System.out.println("拷贝数值类型:");
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
System.out.println("--------------修改后--------------------");
arr2[0] = "纳兰嫣然";
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}
2、Object数组复制
//如果是类型转换问题,获取整形
private static void getIntegerArrayFromObjectArray() {
Object[] obj1 = { 1, 2, 3, "4", "5" };
Integer[] obj2 = new Integer[5];
try {
System.arraycopy(obj1, 0, obj2, 0, obj1.length);
} catch (Exception e) {
System.out.println("transfer exception:"+e);
}
System.out.println(Arrays.toString(obj1));
System.out.println(Arrays.toString(obj2));
}
//获取Object数组中的字符串类型数据
private static void getStringArrayFromObjectArray1() {
Object[] obj3 = { 1, 2, 3, "4", "5" };
String[] obj4 = new String[5];
try {
System.arraycopy(obj3, 2, obj4, 2, 3);
} catch (Exception e) {
//transfer exception:java.lang.ArrayStoreException
System.out.println("transfer exception:"+e);
}
System.out.println(Arrays.toString(obj3));
//[null, null, null, null, null]
System.out.println(Arrays.toString(obj4));
}
//获取Object数组中的字符串类型数据
private static void getStringArrayFromObjectArray2() {
Object[] obj3 = { 1, 2, 3, "4", "5" };
String[] obj4 = new String[5];
try {
System.arraycopy(obj3, 3, obj4, 3, 2);
} catch (Exception e) {
System.out.println("transfer exception:"+e);
}
System.out.println(Arrays.toString(obj3));
//[null, null, null, 4, 5]
System.out.println(Arrays.toString(obj4));
obj3[3] = "zhangssan";
System.out.println("查看是浅复制还是深复制~~~~~");
System.out.println(Arrays.toString(obj3));
System.out.println(Arrays.toString(obj4));
}
3、多维数组复制
//多维数组
public static void twoArray() {
int[] arr1 = {1, 2};
int[] arr2 = {3, 4};
int[] arr3 = {5, 6};
int[][] src = new int[][]{arr1, arr2, arr3};
print("原始模样:", src);
int[][] dest = new int[3][];
System.arraycopy(src, 0, dest, 0, 3);
System.out.println("改变前");
print("src = ", src);
print("dest = ", dest);
//原数组改变后观察新数组是否改变,改变->浅复制,不改变->深复制
src[0][0] = -1;
System.out.println("改变后");
print("src = ", src);
print("dest = ", dest);
}
4、对象数组复制
//对象复制
private static void beanCopy() {
User user1 = new User("zs",18);
User user2 = new User("ls",18);
User user3 = new User("ww",18);
User[] userArraySrc = {user1, user2, user3};
User[] userArrayDest = new User[3];
System.out.println("对象复制,原始模样:"+Arrays.toString(userArraySrc));
System.arraycopy(userArraySrc, 0, userArrayDest, 0, userArraySrc.length);
System.out.println("userArrayDest," + Arrays.toString(userArrayDest));
System.out.println("------------改变原对象------------------");
userArraySrc[0] = new User("su",20);
System.out.println("***" + Arrays.toString(userArraySrc));
System.out.println("***" + Arrays.toString(userArrayDest));
}
//二维数组toString()
private static void print(String string, int[][] arr) {
System.out.print(string);
for (int[] a : arr) {
for (int i : a) {
System.out.print(i + " ");
}
System.out.print(",");
}
System.out.println();
}
三、clone()
protected native Object clone() throws CloneNotSupportedException;
clone()也是native方法,所以也是C语言实现的。
1、基本数组类型clone()
2、对象数组clone()
四、数组排序
可以使用Arrays.sort()方法。
package com.nezha.javase;
import java.util.Arrays;
public class ArrayTest {
public static void main(String[] args) {
int[] a={2,4,-11,9,0};
Arrays.sort(a);
for(int i=0;i<a.length;i++){
System.out.print(a[i]+" ");//-11 0 2 4 9
}
}
}
添加微信,备注1024,赠送Java学习路线思维导图
以上是关于Java零基础入门 8:Java数组的主要内容,如果未能解决你的问题,请参考以下文章