Day12---学习Java第二弹
2021-07-22
数组与字符串
数组(补充)
二维数组中,a.length表示行数,a[i].length表示第i行的列数
动态初始化时,多维数组的行数不可省略,列数可省略
数组与方法互操作
给一个方法中传入数组类型,在引用传递的情况下,如果新数组对值进行改变,则原数组的值也随之改变。
扩展数组值:
public class ArrayExpend {
public static void main(String[] args) {
int[] a = new int[]{1, 2, 3, 4};
System.out.println("数组a:");
arrayPrint(a);
int[] result1 = arrayExpend(a);
System.out.println("扩展结果:");
arrayPrint(result1);
System.out.println("after数组a:");
arrayPrint(a);
int[] result2 = arrayExpend2(a);
System.out.println("扩展结果:");
arrayPrint(result2);
System.out.println("after数组a:");
arrayPrint(a);
}
public static int[] arrayExpend(int[] a) {
if (a == null) {
return new int[]{};
}
//数组引用传递
int[] temp = a;
for (int i = 0; i < a.length; i++) {
temp[i] = temp[i] * 5;
}
return temp;
}
public static int[] arrayExpend2(int[] a) {
if (a == null) {
return new int[]{};
}
//空间重新分配
int[] temp = new int[a.length];
for (int i = 0; i < a.length; i++) {
temp[i] = a[i] * 4;
}
return temp;
}
public static void arrayPrint(int[] c) {
if (c == null) {
return;
}
for (int j = 0; j < c.length; j++) {
System.out.print(c[j] + " ");
}
System.out.println();
}
}
数字升序
java.util.Arrays.sort(arrayName)
import java.util.Arrays;
public class ArrayUtil {
public static void main(String[] args) {
int[] a = new int[]{1, 33, 5, 44, 76};//1,5,33,44,76
System.out.println("排序之前:");
arrayPrint(a);
Arrays.sort(a);
System.out.println("排序之后:");
//改变原数组
arrayPrint(a);
}
public static void arrayPrint(int[] c) {
if (c == null) {
return;
}
for (int j = 0; j < c.length; j++) {
System.out.print(c[j] + " ");
}
System.out.println();
}
}
将指定源数组中的数组从指定位置复制到目标数组的指定位置。
java.lang.System.arraycopy(Object src,int srcPos,Object dest, int destPos,int length);
public class ArrayUtil {
//拷贝
public static void main(String[] args) {
int[] src = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
int[] dest = new int[3];
arrayPrint(src);
arrayPrint(dest);
//方法一
// for(int i=2;i<5;i++){
// dest[i-2]=src[i];
// }
//方法二
System.arraycopy(src, 2, dest, 0, 3);
arrayPrint(src);
arrayPrint(dest);
}
public static void arrayPrint(int[] c) {
if (c == null) {
return;
}
for (int j = 0; j < c.length; j++) {
System.out.print(c[j] + " ");
}
System.out.println();
}
}
复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
java.util.Arrays.copyOf(源数组名称,新数组长度)
import java.util.Arrays;
public class ArrayUtil {
public static void main(String[] args) {
int[] src = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
arrayPrint(src);
int[] dest1 = Arrays.copyOf(src, 7);
//Arrays.copyOf不改变原数组
arrayPrint(src);
arrayPrint(dest1);
int[] dest2 = Arrays.copyOf(src, 10);
arrayPrint(dest2);
}
public static void arrayPrint(int[] c) {
if (c == null) {
return;
}
for (int j = 0; j < c.length; j++) {
System.out.print(c[j] + " ");
}
System.out.println();
}
}
对象数组
初始化
//动态初始化
类名称[] 对象数组名称 = new 类名称[长度];
//静态初始化
类名称[] 对象数组名称 = new 类名称[] {};
public class ArrayOfObjects {
//类方法
public static void printArray(Person[] persons) {
for (int i = 0; i < persons.length; i++) {
System.out.println(persons[i]);
}
}
public static void main(String[] args) {
Person person = new Person(1, "唐僧");//email=null; phone=null;
System.out.println(person);//person.toString(); ——继承自Object
String s = "Bonjour"; //引用类型
System.out.println(s); //s.toString(); ——继承
----------------------------------------------------------------------------------------------------------------
//动态初始化
Person[] persons = new Person[3];
persons[0] = new Person(1, "Jack");
persons[1] = new Person(2, "Tom", "tom@gmail.com");
persons[2] = new Person(3, "Alice", "alice@gmail.com", "15265478955");
ArrayOfObjects.printArray(persons);
//静态初始化
Person[] persons2 = new Person[]{
new Person(4, "Tony")
};
ArrayOfObjects.printArray(persons2);
}
}
class Person {
private int id;
private String name;
private String email;
private String phone;
//构造方法
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public Person(int id, String name, String email) {
this(id, name);
this.email = email;
}
public Person(int id, String name, String email, String phone) {
this(id, name, email);
this.phone = phone;
}
---------------------------------------------------------------------------------------------------------------
//getter方法
public int getId() {
return this.id;
}
public String getName() {
return this.name;
}
public String getEmail() {
return this.email;
}
public String getPhone() {
return this.phone;
}
--------------------------------------------------------------------------------------------------------------
//setter方法
public void setEmail(String email) {
this.email = email;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String toString() {
return " 编号:" + this.id + " 姓名:" + this.name + " 邮箱:" + this.email + " 电话:" + this.phone;
}
}