【Java】迭代的几种控制方法

Java中的基本类型:
在程序设计中经常用到一系列基本类型,它们需要特殊对待。尅把它们想象成“基本”类型。之所以特殊对待,是因为new将对象存储在“堆”里,故用new创建一个对象——特别是小的,简单的变量,往往不是很有效。i对于这些类型,Java采取与C和C++相同的办法。也就是说,不用new来创建变量,而是创建一个并非是引用的“自动”变量。这个变量直接存储“值”,并置于堆栈中,因此更加高效。
Java中的数组:
当创建一个数组对象时,实际上就是创建了一个引用数组,并且每个引用都会自动被初始化为一个特定值,该值拥有自己的关键字null,一旦java看到null,就知道这个引用还没有指向某个对象。在使用任何引用前,必须为其指定一个对象,如果试图使用一个还是null的引用,在运行时将会报错。因此,常犯的数组错误在Java中就可以避免。
编码风格:Java更倾向于“驼峰风格”。
“=”:赋值的是 引用

Java中的迭代
while
public class WhileTest {
       static boolean condition(){
             boolean result = Math.random() < 0.99;
            System. out .println(result + "." );
             return result;
      }
      
       public static void main(String[] args){
             while (condition())
                  System. out .println("Inside 'while.'" );
            System. out .println("exited 'while'." );
      }
}
do-while
do
     statement
while(boolean-expression);
for(initialization; Boolean-expression; step)
public class ListCharacters {
       public static void main(String[] args){
             for (char c = 0; c < 128; c++)
                   if (Character.isLowerCase(c))
                        System. out .println(" character: " + c);
      }
}
foreach用法
public class ForEachString {
       public static void main(String[] args){
             for (char c : "An American Swallow".toCharArray())
                   if (c != ' ' )
                        System. out .println(c + " " );
      }
}
break 和 continue
public class ContinueAndBreak {
	public static void main(String[] args){
		for(int i = 0; i < 100; i++)
		{
			if(74 == i)		break;
			if(i % 9 != 0)	continue;
			System.out.print(" "+i);
		}
		System.out.println();
		int[] arrage = new int[100];
		for(int i = 0; i < 100; i++)
			arrage[i] = i;
		for(int i : arrage)
		{
			if(74 == i)		break;
			if(i % 9 != 0)	continue;
			System.out.print(" "+i);
		}
	}
}
输出: 0 9 18 27 36 45 54 63 72
              0 9 18 27 36 45 54 63 72

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值