数组练习

练习一:

package com.test;
//1、创建时,数组长度的应用
//2、数组的长度打印
public class test1 {
public static void main(String[] args) {
	int x=3;
	int[]numbers =new int[x+2];//5
	x=6;
	System.out.println("x is"+x);//6
	System.out.println("the size of numbers is"+numbers.length);//数组的长度
}
}

练习二:

package com.test;
//for(初始化条件;循环条件;迭代因子){//循环体}
//双层循环
public class test2 {
	public static void main(String[] args) {
		int []list = {1,2,3,4,5,6};//数组创建,长度为6
		for(int i =1;i<list.length;i++) //i为下标,为什么不从0开始?
			list[i]=list[i-1];//前一个数组元素的值赋给后一个数组元素
		for(int i =0;i<list.length;i++)//正序遍历输出
			System.out.println(list[i]+" ");
	}
}

练习三:

package com.test;
//java.lang这个包里,不需要导包,可以直接使用,使用的Math类,调用的random(),随机数
//Math.PI
//1、数组的声明
//2、数组的创建
//double []num1 =new double [100];
//随机数
public class test3 {
public static void main(String[] args) {
//	double []num;//正确的声明,是不能指定长度的
//	num = new double [100];//创建
	double []num =new double [100];
	for(int i =0;i<num.length;i++)//数组的长度
	{	num[i]=Math.random()*100;//数组的引用
	System.out.println(num[i]);
}
}
}

练习四:

package com.test;
//54321
//交换
public class test4 {
public static void main(String[] args) {
	int []list= {1,2,3,4,5};
	for(int i =0,j=list.length-1;i<j;i++,j--) {
		int temp = list[i];//交换
		list[i]=list[j];
		list[j]=temp;
//		System.out.println(list[i]+" ");
}
	for(int i=0;i<list.length;i++)
		System.out.println(list[i]+" ");
}
}

练习五:

package com.test;
//联系方式:
//数组元素的引用
//增强for for each
public class test5 {
public static void main(String[] args) {
	int[] arr =new int[]{1,3,5,0,4,6};
	int[] index =new int[]{0,1,3,2,3,4,2,0,0,5,5};
	String tel =" ";
	for(int i :index) {//for each
		tel +=arr[i];
	}
	//tel=" "+ 1
//	" "+ 1+arr[1]=" "+ 1+3
	System.out.println("联系方式:"+tel);

}
}

练习六:

package com.test;
//二维数组:有两个下标[][]
//二位数组:一个一维数组,里面再放了一个一维数组
//[][],
//声明: 数据类型 [][]array;声明的时候,不能定义长度
//三种写法:1、数据类型 [][]数组名;
//2、数据类型 []数组名[];
//3、数据类型 数组名[][];
//创建:数据类型 [][]数组名 =new 数据类型[index1][index2];
//只指定一个长度:数据类型 [][]数组名 =new 数据类型[index1][];
// 创建二维数组:int [][]array =new int[3][5];
//二维数组,无论是第一维数组还是第二维数组,下标的长度仍然是从0开始,length-1结束
//你得看清楚下标的长度
public class test6 {
public static void main(String[] args) {
	int[][]arry= {{1,2},{3,4},{5,6}};//二维数组的初始化
//	System.out.println(arry[0][0]);//二维数组的引用
//	System.out.println(arry[1][0]);
//	System.out.println(arry[2][0]);
	int sum =0;
	for(int i= 0;i<arry.length;i++)
	
		
	System.out.println(sum);

}
}

练习七:

package com.test;

public class test7 {
public static void main(String[] args) {
	String foo ="blue";
	boolean []bar =new boolean[1];
	System.out.println(bar[0]);
//	if (bar[0]) {
//		foo="green";
//	}System.out.println(foo);

}
}

练习八:

package com.test;

public class test8 {
	public static void main(String[] args) {
		int []a= {1,2};//实参
		swap(a);
		System.out.println("a[0]="+a[0]+",a[1]="+a[1]);}
		
	public static void swap(int[]a) {//数组是可以作为参数使用的
		int temp =a[0];
		a[0]=a[1];
		a[1]=temp;
		}

}

练习九:

package test2;

public class test9 {
public static void main(String[] args) {
	int index =1;
	int foo[]=new int[3];//数组没有赋值的情况下,它的默认值
	int bar =foo[index];//引用数组元素,int bar=foo[1]=0 
	int baz =bar+index;//0+1
	System.out.println(baz);

}
}

练习十:

package test2;

public class test10 {
public static void main(String[] args) {
	float f1[],f2[];
	f1 =new float[10];
	f2 =f1;
	System.out.println("f2[0]="+f2[0]);
//f2[0]=0.0

}
}

练习十一:

package test2;
//1、综合的题,注意方法的调用,跳转的步骤清楚
//2、数组是可以做为参数的
//3、打印输出要进行原样输出:a[0]=1
public class test11 {
	public static void main(String[] args) {
		int []a=new int[1];//动态初始化数组,长度为1
		modify(a);//调用了一个方法,里面的参数是a,直接转到9行
		System.out.println("a[0]="+a[0]);
		}
		public static void modify(int []a) {//数组是可以作为参数的
			a[0]++;//直接转到7行
		}

}

练习十二:

package test2;
//调用了方法,不一定代表就有打印输出
//【从哪调用,就返回哪】
//数组是可以做为参数
//【整个程序的执行步骤】
public class test12 {
	public static void main(String[] args) {
		int []array= {1,2,3,4,5};//1、创建并初始化,5个数组元素
		printfArray(array);//2、调用方法,跳转到17行,array才是实参
		modify(array);
		printfArray(array);
	}
	//7、
	static void modify(int []a)
	{
		for (int i = 0; i < a.length; i++) {
			a[i]= a[i]*i;//0,1,2,____a[0]=1;a[1]=2
		}
	}
	static void printfArray(int []a)//3、从17行开始,数组是可以做为参数,int []a 是形参,实参在哪?
	{
		for (int i = 0; i < a.length; i++) {//4、长度为5
			System.out.print(a[i]+"\t");//5、打印输出后
			
		}System.out.println();//6、换行,跳转到【从哪调用,就返回哪】
	}

}

练习十三:

package test2;
//注意在使用for循环的时候,花括号的位置,for的循环条件
public class test13 {
	public static void main(String[] args) {
		int []array= {1,2,3,4,5};
		printArray(array);
		for (int i = 0; i < array.length; i++) 
			modify(array[i],i);
			printArray(array);
		}
		static void modify(int a,int i) {
			a=a*i;
			}
		static void printArray(int []a) {
			for (int i = 0; i < a.length; i++)
				System.out.print(a[i]+"\t");
				System.out.println();
		}	
		//1	2	3	4	5		
		//1	2	3	4	5	
}

练习十四:

package test2;
//二维数组在进行创建时候,可以只给第一维分配空间
//二维数组,相当于是第一维的数组元素,刚好里面也是一个一维数组
//对于双层for 循环的使用,注意一下:初始化,循环条件,迭代因子
public class test14 {
public static void main(String[] args) {
	int a[][]=new int [4][];//创建一个二维数组a,为第一维分配了空间,长度为4,
	a[0]=new int[1];//第一维数组的0号元素,里面有1个数组元素,动态初始化
	a[1]=new int[2];//第一维数组的1号元素,里面有2个数组元素
	a[2]=new int[3];//第一维数组的2号元素,里面有3个数组元素
	a[3]=new int[4];//第一维数组的3号元素,里面有4个数组元素
	int i,j,k=0;
	for (i = 0; i < 4; i++) //外循环代表的就是我们的行
		for (j = 0; j < i+1; j++) {//内循环代表列,j代表的是该行所有具有的个数
//			j的取值是有0,1,2,3
//			第0行,有1个元素→i+1=j
//			第1行,有2个元素
//			第2行,有3个元素
//			第3行,有4个元素
			a[i][j]=k;
			k++;
		}
		for (i = 0; i < 4; i++) {
			for (j = 0; j < i+1; j++) 
				System.out.print(a[i][j]+" ");
				System.out.println();
	
		}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值