黑马程序员 JAVA基础(三)

                               -----------android培训java培训java学习型技术博客、期待与您交流!------------

              在此,分享一下自己学习JAVA的学习心得。有不对的地方请帮忙改正,也希望对想java的同学有帮助!



JAVA语言基础


函数

定义:函数就是定义在类中具有特定功能的一段独立小程序,函数也称方法。

格式:

      修饰符  返回值类型  函数名 (参数类型  形式参数1,参数类型   形式参数2 )

      {
                 执行语句;
                 return   返回值;
      }

注:返回值类型:函数运行后的结果的数据类型;
       参数类型:是形式参数的数据类型;
       形式参数:是一个变量,用于存储调用函数时传递给函数的实际参数;
       实际参数:传递给形式参数的具体数值;
       return:用于结束函数;
       返回值:该值会返回给调用者。

 函数的特点:

(1)定义函数可以将功能代码进行封装;
(2)函数只有被调用才会执行;
(3)函数的出现提高了代码的复用性;
(4)对于函数没有具体返回值的情况,返回值类型用关键字void表示,那么如果该函数的return语句在最后一行可以省略不写。

注:函数中只能调用函数,不可以在函数内部定义函数;
  
JAVA练习代码:
  
class BabySitter
{
	String name = "张晓";

	String sex = "女";

	String age = "29";

	void cooking()
	{
	   System.out.println(name+"~"+sex+"~"+age);
	}

}


class ClassDemo 
{
	public static void main(String[] args) 
	{
		BabySitter B = new BabySitter();

		B.cooking();

	}
}

class Trans 
{
	public static void main(String[] args) 
	{
       tobin (60);
	   toba(60);
	   tohex(60);
		
	}

	//十进制转二进制
	public static void tobin(int num)
	{
	    trans(num ,1,1);
	}
	//十进制转八进制
	public static void toba(int num)
	{
	    trans(num ,7,3);
	}
	//十进制转十六进制
	public static void tohex(int num)
	{
	    trans(num ,15,4);
	}
	public static void trans(int num,int base,int offset)
	{
		if(num==0)
		{
		  System.out.println(0);
		}
		//定义一个表
	   char [] chs = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

	   //定义一个容器
	   char [] arr =new char[32];

	   int pos = arr.length;//位移位数

       while(num!=0)
		{
		   int temp= num & base;   

		   arr[--pos] = chs[temp];

		   num= num>>>offset;
	   }

	   for(int x= pos; x<arr.length; x++)
		{
	      System.out.print(arr[x]);
	    }
        System.out.println();
	}
}


</pre></div><div><span style="font-size:18px">在主函数中调用打印99乘法表的函数</span><pre name="code" class="java">class FunctionDemo
{

   public static void main (String args[]) 

   {
     printer99();//打印99乘法表
    } 
    public static void printer99()
    {
       for(int x=1; x<=9; x++)
       {
          for(int y=1; y<=x;y++)
          {
             System.out.print(x+"*"+y+"="+x*y+"\t");
            }
            System.out.println();
        }
     }
}

                  
函数的重载(overload)
 
       
        

Java练习代码:

函数 person(String name)与 person(String name,String sex,int age)属于重载关系

class person
{
	private String name;
	private int age;
	private String sex;

        person(String name)
	{
	   this.name = name;
	
	}
	person(String name,String sex,int age)
	{
	   this.name = name;

	   this.sex = sex;

	   this.age = age;	
	}

	public void speak()
	{
	   System.out.println(this.name+" "+this.sex+" "+this.age);
	}
}

class PersonDemo
{
	public static void main(String[] args) 
	{
	      person p = new person();
          
		  p.name = "jolin";
 
   		  p.sex = "woman";

		  p.age = "36";

		  speak();

	}
}



数组

定义:同一类型数据的集合。其实数组就是一个容器。
一维数组
格式1:
元素类型[ ]  数组名  =  new  元素类型  [元素个数或者数组长度]
示例: int   [  ]  arr   =  new  int  [5];

 
格式2:
元素类型[ ]  数组名  =  new  元素类型  [ ]  {元素,元素,  元素,  元素,  .......  }
示例: int   [  ]  arr   =  new  int  [  ] {3,5,7,9} ;
           int   [  ]  arr  =  {3,5,7,9};


数组内存结构

    
数组常见操作问题:
(1)数组角标越界异常(ArrayIndexOutOfBoundsException)
        int  [  ]   arr  =  new  int [2];
        System.out.println(arr[3]);
              访问到了数组中的不存在的角标时发生
(2)空指针异常(NullPointerException)
        int [  ]  arr  = null;      
        System.out.println(arr[0]);
        arr引用没有指向实体,却在操作实体中的元素时。

数组中常见的操作
(1)获取最值(最大值,最小值)
Java练习代码:
class ArrayDemo
{
	public static void main(String []args)
	{
	   int [] shuzu= {1,5,2,3,6,9,4,7};  
	   //打印数组
       printer(shuzu);
	   //从大到小排序
       Dn(shuzu);
	   printer(shuzu);
	   //从小到大排序
	   Up(shuzu);
	   printer(shuzu);
	}
	
	//定义一个函数为Dn,要求数组按从大到小输出

	public static void Dn (int arr[])
	{
	    for (int x=0; x<arr.length-1 ;x++ )
	    {
			for(int y=x+1; y<arr.length ;y++)
			{
			   if (arr[x]<arr[y])
				{
			       int temp=arr[x];
				   arr[x]=arr[y];
			       arr[y]=temp;
			    }		
			}
	    }
	
	}

	//定义一个函数为Up,要求数组从小到大排序

		public static void Up (int arr[])
	{
	    for (int x=0; x<arr.length-1 ;x++ )
	    {
			for(int y=x+1; y<arr.length ;y++)
			{
			   if (arr[x]>arr[y])
				{
			       int temp=arr[x];
				   arr[x]=arr[y];
			       arr[y]=temp;
			    }		
			}
	    }
	}

	//定义一个打印数组的函数printer。

	public static void printer (int arr[])
	{
	   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]+"]"); 
	   }
	   
	}

}


(2)排序(选择排序,冒泡排序)
class BublueDemo 
{
	public static void main(String[] args) 
	{
		int [] arr ={0,2,3,5,1,9,4};
 
		bublue(arr);
		printer(arr);
	}

	public static void bublue(int []arr)
	{
	   for(int x=0; x<arr.length;x++)
		  {
		     for(int y=0;y<arr.length-1-x;y++) //-x:每次比较的元素减少;-1:避免角标越界。
			 {
		        if(arr[y]>arr[y+1])
				 {
			       int temp=arr[y];
				   arr[y]=arr[y+1];
				   arr[y+1]=temp;
			     }
		     }
	      }
	}

	public static void printer (int arr[])
	{
	   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]+"]"); 
	   }
	   
	}
}


(3)折半查找(二分查找)

二维数组





















































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值