黑马程序员—数组

-------Android培训java培训、期待与您交流! ----------


一、数组

数组是同一种数据类型的集合,其实数组就是一个容器。

 
 数组有两种定义格式:
 1.int[] arr=new int[3];
 2.int[] arr=new int[]{1,2,5}; 或  int[] arr={1,2,5};


定义功能:打印数组中的每一个元素

class Array
{
	public static void main(String[] args)
	{
		int[] arr=new int[]{1,2,3,4,5};
		for(int x=0;x<5;x++)
		{
			System.out.println(arr[x]);
		}
	}
}

定义功能:计算数组中的所有元素和

class Array1
{
	public static void main(String[] args)
	{
		int sum=0;
		int[] arr=new int[]{1,2,3,4,5};
		for(int x=0;x<5;x++)
		{
			sum+=arr[x];
			System.out.println(arr[x]);
		}
		System.out.println("sum="+sum);
	}
}
</pre><p></p><p>定义功能:用于打印数组中的元素,元素间用逗号隔开。</p><p></p><pre code_snippet_id="303171" snippet_file_name="blog_20140420_4_3642529" name="code" class="java">class Array2
{
	public static void main(String[] args)
	{
		int[] arr={3,9,6,4,-3,7};
		for(int x=0;x<arr.length;x++)
		{
			if(x!=arr.length-1)
				System.out.print(arr[x]+",");
			else
				System.out.println(arr[x]+".");
		}	
	}
}

定义功能:用于打印数组中的元素,元素间用逗号隔开,并用中括号括起来。

class Array3
{
	public static void main(String[] args)
	{
		int[] arr={3,9,6,4,-3,7};
		System.out.print("[");
		for(int x=0;x<arr.length;x++)
		{
			if(x!=arr.length-1)
				System.out.print(arr[x]+",");
			else
				System.out.println(arr[x]+"]");
		}	
	}
}



定义功能:获取最大值(法一)
boolean型数组的默认值是faulse。

class Test3
{
	public static int FTT(int[] dest3)
	{
		int max=dest3[0];
		
		for(int x=1;x<dest3.length;x++)
		{
			if(dest3[x]>max)
				max=dest3[x];	
		}
		return max;
	}
	
	
	public static void main(String[] args)
	{
		int[] dest3={-6,-3,-8,4,-9,-5};
		int max=FTT(dest3);
		System.out.println("max="+max);
	}
}

定义功能:获取最大值(法二)

class Test4
{
	public static int FTT(int[] dest)
	{
		int max=0;
		for(int x=1;x<dest.length;x++)
		{
			if(dest[x]>dest[max])
				max=x;
		}
		return dest[max];
	}
	
	
	public static void main(String[] args)
	{
		int[] dest={3,5,1,6,-9,0,7,8};
		int max=FTT(dest);
		System.out.println("max="+max);
	}
}


定义功能:选择排序:selectSort(法一)
选择排序特点:内循环结束一次,最值出现在头角标位置上。

class Test1
{
	public static void main(String[] args)
	{
		int[] Dest={3,2,0,4,-8,7};
		Arrays.sort(Dest);
		
		int length=Dest.length;
		for(int x=0;x<length;x++)
		{
			for(int y=x+1;y<length;y++)
				if(Dest[x]>Dest[y])
				{
					int temp=Dest[x];
					Dest[x]=Dest[y];
					Dest[y]=temp;
				}
			if(x!=length-1)
				System.out.print(Dest[x]+",");
			else 
				System.out.println(Dest[x]+".");
		}
	}
}

定义功能:冒泡排序:bubbleSort
冒泡排序特点:相邻的两个元素进行比较,如果符合条件换位。
运行第一圈最值出现在了最后位。
最快的排序:希尔排序

class Test5
{
	public static void bubbleSort(int[] desk)
	{
		for(int x=1;x<desk.length;x++)
		{
			for(int y=0;y<desk.length-x;y++)
			{
				if(desk[y]>desk[y+1])
				{
				    int temp=desk[y];
					desk[y]=desk[y+1];
					desk[y+1]=temp;
				}
			}
		}
	}
	public static void print(int[] desk)
	{
		System.out.print("[");
		for(int x=0;x<desk.length;x++)
		{
			if(x!=desk.length-1)
				System.out.print(desk[x]+",");
			else 
				System.out.println(desk[x]+"]");
		}
	}
	public static void main(String[] args)
	{
		int[] desk={9,-8,6,8,4,0,7};
		print(desk);
		bubbleSort(desk);
		print(desk);	
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值