| 本帖最后由 王琪 于 2014-2-28 01:29 编辑 
 我想了好像java不像C语言             1.C语言                                                                                             2java
 #include <stdio.h>                                                                         class java{
 void fun(char p[] ){//char *p                                                         void fun(  char [] p ){
 char code[]={'4','5','6','7'};                                                            char []code={'4','5','6','7'};
 p=code//此时p所指的对象本身发生了变化                                     p=code;   ///应该只是对象句柄转移
 }                //如果参数改为char p[4]是传值像java的克隆                   }                   //不会干扰传来的a数组
 void main(){                                                                                   public static void main(String[]args){
 char a[]={'1','2','3','4'};                                                                  char [] a={'1','2','3','4'};
 fun(  a );     printf("%s",a)   // 输出4567                                   fun( a );  System.out.println(new String(a));  //输出1234
 }                                                                                                    }  }//System.out.println(a);将输出乱码地址
 
 
 2 所以通过函数改变的方法:(1)for循环通过索引一个一个改变(2)通过 System类的arraycopy静态函数用于数组拷贝
 (1)for( ; ; ){}
 
 缺点是a.length太大 c的小  或者谁的大谁的小都不知道是怎么处理,String bite char相互转换复制怎么办 一个.length 好像办不到复制代码void bain(int [] a,int [] c){
for (int i = 0; i < a.length; i++) {
c[i]=a[i];
}
}
(2) for(高级)
 这个我想了但是我也不会用,也查了资料希望对大家有用
 
 
 
 复制代码<b>普通的for循环:
public class test {
public static void main(String[] args) {
    int a[]={0,1,2,3,4,5,6,7,8,9};
    for(int i=0;i<a.length;i++){
     System.out.print(a[i]+" ");
    }
}
}
增强型的for循环:
public class test {
public static void main(String[] args) {
int a[]={0,1,2,3,4,5,6,7,8,9};
     for(int i :a){
      System.out.print(i+" ");
     }
}
}
在上面这个例子 增强型的for循环 和普通for循环一样
增强型的for循环 优点主要体现在集合中,随便举个例子
比如对 set 的遍历
一般是迭代遍历:
Set<String> set = new HashSet<String>();
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String str = it.next();
System.out.println(str);
}
for循环遍历:
for (String str : set) {
System.out.println(str);
}
是不是简单些?
优点还体现在泛型 假如 set中存放的是Object
Set<Object> set = new HashSet<Object>();
for循环遍历:
for (Object obj: set) {
if(obj instanceof Integer){
int aa= (Integer)obj;
}else if(obj instanceof String){
String aa = (String)obj
}
........
}
如果你用Iterator遍历,那就晕了
map list 也一样
唯一的缺点就是 在遍历 集合过程中,不能对集合本身进行操作
for (String str : set) {
set.remove(str);//错误!
}</b>
 (我连夜做的)但字数太多就不让发表了就分开发了
 
 
 
 |