zoukankan      html  css  js  c++  java
  • Java数组练习题小结

    //2015/07/07
    //Java数组小小练习题
    /*
    3. 写一个函数,计算一个整数数组的平均值
    4. 自定义一个整数数组a,读入一个整数n,如果n 在数组中存在,则输出n 的下标;如果不存在,则输出-1。
    5. 给定一个数组,输出数组中的最大值和最小值
    6. *给定一个数组,把这个数组中所有元素顺序进行颠倒。
    7. *完成数组的冒泡排序算法:给定一个数组:int[] a = {1,3,2,7,5},利用冒泡排序对其按照从小到大的顺序排序,然后输出结果。
    8. *使用第二种算法对数组进行排序
    注:每一题都被函数封装起来了
    */
    package Chp5;
    
    public class ArrayTest {
    	//Three 计算数组的平均值
    	public void Average(int[] array)
    	{
    		System.out.println("第3题");
    		int len = array.length;
    		int average = 0;
    		int sum = 0;
    		for(int i = 0;i<len;i++)
    		{
    			sum+=array[i];
    		}
    		average = sum/len;
    		System.out.println("数组的平均值为:"+average);
    	}
    	
    	//第四题:自定义一个整数数组a,读入一个整数n
    	public void IsExit(int[] array,int num)
    	{
    		System.out.println("第4题");
    		int len = array.length;
    		boolean bl = false;
    		for(int i = 0;i<len;i++)
    		{
    			if(array[i] == num)
    			{
    				System.out.println("该数存在于数组中,下标为:"+i);
    				bl = true;
    			}
    		}
    		if(bl == false)
    		{
    			System.out.println(-1);
    		}
    	}
    	
    	//5题:求数组中的最大值和最小值
    	public void GetMaxMin(int[] array)
    	{
    		System.out.println("第5题");
    		int len = array.length;
    		int temp;
    		for(int i = 0;i<len;i++)
    		{
    			for(int j = 0;j<len-i-1;j++)
    			{
    				if(array[j]>array[j+1])
    				{
    					temp = array[j];
    					array[j] = array[j+1];
    					array[j+1] = temp;
    				}
    			}
    		}
    		
    		System.out.println("数组最大值为:"+array[len-1]);
    		System.out.println("数组最小值为:"+array[0]);
    		
    	}
    	
    	//第6题:给定一个数组,把这个数组中所有元素顺序进行颠倒。
    	public void UpDownLocation(int[] array)
    	{
    		System.out.println("第6题");
    		int len = array.length;
    		int temp = 0;
    		for(int i = 0;i<len;i++)
    		{
    			temp = array[len-1];
    			array[len-1] = array[i];
    			array[i] = temp;
    			len--;
    		}
    	}
    	
    	///////////////////////////
    	public void PrintArray(int[] array)
    	{
    		int len = array.length;
    		for(int i = 0;i<len;i++)
    		{
    			System.out.print(array[i]+"  ");
    		}
    		System.out.println();
    	}
    	
    	///////////////////////////
    	
    	//第7题:按照从小到大的顺序排序,然后输出结果
    	public void BubbleSort(int[] array)
    	{
    		System.out.println("第7题");
    		int len = array.length;
    		int temp;
            for(int i = 0;i<len;i++)
            {
                for(int j = 0;j<len-i-1;j++)
                {
                    if(array[j]>array[j+1])
                    {
                        temp = array[j];
                        array[j] = array[j+1];
                        array[j+1] = temp;
                    }
                }
            }
    	}
    	
    	//第8题:用第二种算法对数组进行排序
    	public void MyChoiceSort(int array[])
        {
    		System.out.println("第8题");
    		int len = array.length;
            int temp;
            for(int i = 0;i<len;i++)
            {
                for(int j = i+1;j<len;j++)
                {
                    if(array[i]>array[j])
                    {
                        temp = array[j];
                        array[j] = array[j+1];
                        array[j+1] = temp;
                    }
                }
            }
        }
    	
    	public static void main(String[] args) {
    		ArrayTest Myarray = new ArrayTest();
    		////
    		int[] array ={8,4,6,2,2,6,4,8};
    		Myarray.Average(array);
    		////
    		Myarray.IsExit(array, 5);
    		///
    		Myarray.GetMaxMin(array);
    		///
    		Myarray.UpDownLocation(array);
    		Myarray.PrintArray(array);
    		///
    		Myarray.BubbleSort(array);
    		Myarray.PrintArray(array);
    		///
    		Myarray.MyChoiceSort(array);
    		Myarray.PrintArray(array);
    	}
    }
    

      

  • 相关阅读:
    Node.js的安装与配置【转载】
    The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector【端口号被占用】
    MVC登陆认证简单设置
    Winform无边框窗体拖动
    Winform截图小程序
    C#Winform实时更新数据库信息Demo(使用Scoket)
    记DateTime.Now.ToString()遇到的一个坑
    T4模板的一些配置(从EF数据更新)
    设计模式之策略模式
    打造高效的研发组织架构:高效研发流程那些事(一)——读《技术领导力实战笔记》摘要
  • 原文地址:https://www.cnblogs.com/IamJiangXiaoKun/p/4628510.html
Copyright © 2011-2022 走看看