学习笔记 Java_毕向东_流程控制语句_函数_数组 2014.7.30

一、流程控制语句

1、for
  1. 代码:MyForTest3.java
    //1~100之间 7的倍数的个数。并打印。 
    //计数器 
    class MyForTest3 
    { 
    	public static void main(String[] args) 
    	{ 
    		int count = 0; for(int x = 1; x <= 100; x++) 
    		{ 
    			if(x % 7 == 0) 
    			{ 
    				System.out.print("x=" + x + ","); 
    				count++; 
    			} 
    		} 
    		System.out.println("count:" + count);
    	} 	
    }
  2. 代码:MyForForDemo.java
    /*
    需求:打印出
    *****
    ****
    ***
    **
    *
    
    */
    
    class MyForForDemo 
    {
    	public static void main(String[] args) 
    	{
    		for(int x = 0; x < 5; x++)
    		{
    			//for(int y = 0; y < (5 - x); y++)
    			for(int y = x; y < 5; y++)
    			{
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    	}
    }
  3. 代码:MyForForTest.java
    class MyForForTest
    {
    	public static void main(String[] args)
    	{
    		/*
    		*
    		**
    		***
    		****
    		*****
    			
    		*/
    		for(int x = 0; x < 5; x++)
    		{
    			for(int y = 0; y < (x + 1); y++)
    			{
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    
    		//九九乘法表
    		for(int x = 1; x <= 9; x++)
    		{
    			for(int y = 1; y <= x; y++)
    			{
    				System.out.print(y + "*" + x + "=" + (y * x) + "\t");
    			}
    			System.out.println();
    		}
    	}
    }
  4. MyForForTest2.java

    /*
        *
       * *
      * * *
     * * * *
    * * * * *
    
    */
    
    class MyForForTest2
    {
    	public static void main(String[] args)
    	{
    		for(int x = 1; x <= 5; x++)
    		{
    			for(int y = x; y < 5; y++)
    			{
    				System.out.print(" ");
    			}			
    			for(int z = 1; z <= x; z++)
    			{
    				System.out.print("* ");
    			}
    			System.out.println();
    		}
    	}
    }
2、break,continue
  1. break:用于:选择(switch),循环结构。标号 w:for(){for(){break w;}}  //break w,就停止w循环了 
  2. continue:只能用作循环结构。结束本次循环,继续下一次循环
  3. 代码:MyOtherDemo.java
    class MyOtherDemo 
    {
    	public static void main(String[] args) 
    	{
    		wai:for(int x = 1; x <= 5; x++)
    		{
    			for(int y = 1; y <= 3; y++)
    			{
    				
    				System.out.print("*:");
    				//continue wai;
    				//break;
    			}
    			System.out.println(",");
    		}
    	}
    }

 

二、函数

1、函数的特点
  1. void Xxx(){return;}  //如果返回类型是void,该函数内部的return语句如果在最后一行,可以省略
  2. 注意:函数中只能调用函数,不能定义函数。
2、函数的应用
  1. return就代表函数结束了。
  2. 返回值类型为void时,不要输出(System.out.println();)。直接调用就是了:函数名(参数)
  3. 代码:MyFunctionDemo2.java
    class MyFunctionDemo2
    {
    	static int sum(int x, int y)
    	{
    		return x + y;
    	}
    
    	static boolean isEqual(int x, int y)
    	{
    		return x == y;
    	}
    
    	static int max(int x, int y)
    	{
    		return x >= y ? x : y;
    	}
    
    	public static void main(String[] args)
    	{
    		int a = 1, b = 3;
    		System.out.println("sum=" + sum(5,7));
    		System.out.println("a isEqual b:" + isEqual(a, b));
    		System.out.println("a,b max:" + max(a, b));
    	}
    }
  4. 代码:MyFunctionTest.java
    class MyFunctionTest 
    {	
    	static void drawRectangle(int row, int col)
    	{
    		for(int x = 0; x < row; x++)
    		{
    			for(int y = 0; y < col; y++)
    			{
    				System.out.print("*");
    			}
    			System.out.println();
    		}
    	}
    
    	public static void main(String[] args) 
    	{
    		drawRectangle(3, 4);
    	}
    }
3、重载
  1. 特点:与返回类型无关,只看参数列表。

三、数组

1、 数组定义
  1. 定义数组。格式1:代码:MyArrayDemo.java
    class MyArrayDemo 
    {
    	public static void main(String[] args) 
    	{
    		int[] x = new int[3];
    		x[0] = 58;
    		System.out.println(x[0]);
    		x = null;  //只有引用数据类型才能引用这个常量(不知道地址,x就跟数组没有关系了。就会被视为垃圾。在不定时的时间内会启动垃圾回收机制清除它,优化比C++做得好)
    		System.out.println(x);  //得null
    	}
    }
  2. 定义数组。格式2:
    int[] a = new int[]{1, 2, 3, 4};  //new int[]括号里就不写长度了
    int[] b = {5, 9, 3, 2, 1};
2、内存结构
  1. 栈内存:用于存储局部变量(定义在方法中的变量,定义在方法内的参数中的变量,定义在for循环的变量)。数据使用完毕,会自动释放
  2. 堆:new出来的东西都在堆里面。凡是new出来的都叫实体(数组和对象);每一个实体都有内存地址值;实体中的变量都有默认初始化值(boolean型默认是false);实体在不被使用时,会在不确定的时间内被垃圾回收器回收。                      实体和实例是一样的吧?
3、数组是引用数据类型。代码:MyArrayDemo.java
class MyArrayDemo 
{
	public static void main(String[] args) 
	{
		int[] x = new int[3];
		int[] y = x;
		y[1] = 68;
		System.out.println(x[1]);  //得68
		
		x = null;  //此时,对内存中没有垃圾。相当于:同一台电脑(堆),x没用了,但是y还在用
		System.out.println(x); //得null
		System.out.println(y[1]);  //得68
	}
}
4、编译时只检查语法错误
  1. 所以int[] arr = new int[3]; 输出arr[3]在编译时OK,运行时就有ArrayIndexOutOfBoundsException提示了
5、打印数组的元素。代码:MyArrayDemo3.java
class MyArrayDemo3 
{
	//打印数组中的元素。元素间用逗号隔开。
	static void printArray(int[] arr)
	{
		for(int x = 0; x < arr.length; x++)
		{
			if(x != arr.length - 1)  //这样就可以把数组末尾的逗号去掉了
				System.out.print(arr[x] + ",");  
			else
				System.out.print(arr[x]);
		}
		System.out.println();
	}

	public static void main(String[] args) 
	{
		int[] a = new int[]{1,2,3};
		printArray(a);  
		System.out.print(a);  //得[I@16dadf9 打印的是数组的引用
	}
} 
6、最值。代码:MyArrayTest.java
class MyArrayTest 
{
	//获取最大值
	static int max_1(int[] arr)
	{
		int max = arr[0];
		for(int x = 1; x < arr.length; x++)  //如果x=0,0和0比没意识嘛,就让它为1,少比一次
		{
			max = arr[x] > max ? arr[x] : max;
		}
		return max;
	}

	//获取最大值的另一种方法
	static int max_2(int[] arr)
	{
		int max = 0;  //0最为脚标存在
		for(int x = 0; x < arr.length; x++)
		{
			//arr[max] = arr[x] > arr[max] ? arr[x] : arr[max];
			if(arr[x] > arr[max])
				max = x;
		}
		return arr[max];
	}

	static int min(int[] arr)
	{
		int min = arr[0];
		for(int x = 1; x < arr.length; x++)
		{
			min = arr[x] < min ? arr[x] : min;
		}
		return min;
	}

	public static void main(String[] args) 
	{
		int[] a = new int[]{1, 2, 3};
		System.out.println("max=" + max_1(a) + ",min=" + min(a));
		System.out.println("max=" + max_2(a) + ",min=" + min(a));
	}
}
7、排序。(冒泡和选择是面试经常问到的)代码:MyArrayTest2.java
import java.util.*;
//Arrays.sort(arr);//java中已经定义好的一种排序方式。开发中,对数组排序。要使用该句代码。

class MyArrayTest2 
{
	//选择排序(从小到大排序)
	//内循环结束一次,最值出现头角标位置上。
	static void selectSort(int[] arr)
	{
		for(int x = 0; x < arr.length - 1; x++)//x < arr.length; x++)
		{
			for(int y = x + 1; y < arr.length; y++)
			{
				if(arr[x] > arr[y])  //这里换成小于,就是从大到小排序了
				{
					/*
					int temp;
					temp = arr[x];
					arr[x] = arr[y];
					arr[y] = temp;
					*/

					//swap(arr, arr[x], arr[y]);  //为什么调用这个函数会出现ArrayIndexOutOfBoundsException?
					swap(arr, x, y);  //原来是我参数(x和y)传错了
				}
			}  
		}
	}

	//冒泡排序(面试中常遇到的)
	//最值出现在了最后位
	static void bubbleSort(int[] arr)
	{
		for(int x = 0; x < arr.length - 1; x++)
		{
			//for(int y = 0; y < arr.length - 1; y++)
			for(int y = 0; y < (arr.length - 1) - x; y++)  //冒泡一次,就能在后面确定一个元素。所以,每次比较的次数都会减1
			{
				if(arr[y] > arr[y + 1])  
				{
					int temp;
					temp = arr[y];
					arr[y] = arr[y + 1];
					arr[y + 1] = temp;
				}
			}
		}
	}

	static void swap(int[] arr, int a,int b)   //为什么会越界啊? 
	{
		int temp;
		temp = arr[a];
		arr[a] = arr[b];
		arr[b] = temp;
	}

	static void printArray(int[] arr)
	{
		for(int x = 0; x < arr.length; x++)
		{
			if(x != arr.length - 1)
				System.out.print(arr[x] + ",");
			else
				System.out.println(arr[x]);
		}
	}


	public static void main(String[] args) 
	{
//		int[] arr = new int[]{5, 9, 3, 2, 1};
		int[] arr = {5, 9, 3, 2, 1};  //还可以这么定义?
		selectSort(arr);
		printArray(arr);

		bubbleSort(arr);
		printArray(arr);

		int[] a = {5, 9, 3, 2, 1, 7, 5}; 
		Arrays.sort(a);
		printArray(a);
	}
}


 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值