java数组扩容与缩容

public static  T[] dilatationArray(T[] datas,int newlen) {
		//不能为负数
		newlen = newlen<0?0:newlen;
		//生成一个新数组,并copy原值到新数组
		return Arrays.copyOf(datas, newlen);
	}
package testpro;

import java.util.Arrays;

/**
 * 数组扩容缩容
 * 扩容之后扩容部分按照类型默认赋值为0,false或者null
 * @author Administrator
 *
 */
public class ArrayDilatation {
	public static void main(String[] args) {
		Student[] students = new Student[60];
		for (int i=0;i T[] dilatationArray(T[] datas,int newlen) {
		//不能为负数
		newlen = newlen<0?0:newlen;
		//生成一个新数组,并copy原值到新数组
		return Arrays.copyOf(datas, newlen);
	}
}

class Student{
	private String name;
	private String gender;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Student(String name, String gender, int age) {
		super();
		this.name = name;
		this.gender = gender;
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", gender=" + gender + ", age=" + age + "]";
	}
}

 

你可能感兴趣的:(Java)