数组概念
数组是先声明,在开辟空间的,但是开辟完空间后长度是固定的 所以数组一旦创建完成之后,它的长度就不变了 数组没有赋值,但是会有个默认值 * int 类型默认值是 0 * double 是 0.00 * boolean 是false * String 是 null
public class Demo01 {
public static void main(String[] args) {
Random random = new Random();
int[] nums = new int[10];
int i = 0;
while (true) {
int num = random.nextInt(1000);
if(num%3 == 0){
nums[i] = num;
i++;
System.out.println(Arrays.toString(nums));
}
if(i == 10){
break;
}
}
}
}
数组与基本数据类型的区别
/** * 堆栈 * 栈区是先进后出 * main方法执行后,会在栈区分配空间, * 遇到基本数据类型,都会直接在栈区保存数据 (int i = 10) * 遇到引用数据类型,(int[] i = {1,2,3}),现在堆区开辟空间存储数据,然后在栈区保存堆区的地址 / /* * 实现数组的拷贝 * 对于有序数组的插入与删除操作本质就是数组的拷贝 / /* * 数组的插入 */
实现数组的添加数据
@Test
public void test01(){
int[] target;
int[] source = {1, 12, 23, 34};
int num = 56;
int index = -1;
for (int i = 0; i < source.length; i++) {
if(num <= source[i]){
index = i;
break;
}
}
if(index == -1){
target = Arrays.copyOf(source, source.length + 1);
target[source.length] = num;
}else{
target = new int[source.length + 1];
for(int i =0;i<target.length;i++){
if(i < index){
target[i] = source[i];
}else if(i == index){
target[i] = num;
}else if(i > index){
target[i] = source[i - 1];
}
}
}
System.out.println(Arrays.toString(target));
}
通过Arrays和System来实现数据添加
int[] target = new int[20];
int[] source = {1, 12, 23, 34,45,56,67,78};
int num = 40;
int index = 5;
System.arraycopy(source,0,target,0,5);
target[index] = num;
System.arraycopy(source,index,target,index+1,source.length - index);
System.out.println(Arrays.toString(target));
@Test
public void test02(){
int[] target;
int[] source = {1, 12, 23, 34};
int num = 2;
int index = -1;
for (int i = 0; i < source.length; i++) {
if(num <= source[i]){
index = i;
break;
}
}
if(index == -1){
target = Arrays.copyOf(source, source.length + 1);
target[source.length] = num;
}else{
target = new int[source.length + 1];
System.arraycopy(source,0,target,0,index);
target[index] = num;
System.arraycopy(source,index,target,index+1,source.length - index);
}
System.out.println(Arrays.toString(target));
}
数组的冒泡排序
@Test
public void test03(){
int[] nums = {1, 34, 23, 12, 90, 35};
int temp = 0;
for(int i=0; i<nums.length - 1; i++){
for(int j = 0; j< nums.length - 1 - i; j++){
if(nums[j] > nums[j+1]){
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(nums));
}
数组案例实操
/** * 使用数组保存,提前先初始化一部分数据,数组长度不可以变 * 然后输入字符串,动态插入,如果数组长度不够,可以动态扩容 */
public Boolean checkStringArray(String[] names){
for (String name : names) {
if ("".equals(name) || name == null) {
return true;
}
}
return false;
}
@Test
public void test04(){
String[] names = {"hehe", "zhangsan", "lisi", "wangcai"};
String[] newNames;
String next;
Arrays.toString(names);
System.out.println(Arrays.toString(names));
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入数据:");
next = scanner.next();
if(checkStringArray(names)){
for(int i=0; i<names.length;i++){
if(names[i] == null || names[i].equals("")){
names[i] = next;
break;
}
}
}else{
newNames = new String[names.length + (Integer)names.length/2];
System.arraycopy(names, 0, newNames, 0, names.length);
for(int i =0;i<newNames.length;i++){
if(newNames[i] == null || newNames[i].equals("")){
newNames[i] = next;
break;
}
}
names = newNames;
}
System.out.println(Arrays.toString(names));
}
}
|