A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王琪 中级黑马   /  2014-2-28 01:25  /  1142 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 王琪 于 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( ; ; ){}
  1. void bain(int [] a,int [] c){
  2. for (int i = 0; i < a.length; i++) {
  3. c[i]=a[i];
  4. }
  5. }
复制代码
缺点是a.length太大 c的小  或者谁的大谁的小都不知道是怎么处理,String bite char相互转换复制怎么办 一个.length 好像办不到

(2) for(高级)
这个我想了但是我也不会用,也查了资料希望对大家有用



  1. <b>普通的for循环:
  2. public class test {
  3. public static void main(String[] args) {
  4.     int a[]={0,1,2,3,4,5,6,7,8,9};
  5.     for(int i=0;i<a.length;i++){
  6.      System.out.print(a[i]+" ");
  7.     }
  8. }
  9. }
  10. 增强型的for循环:
  11. public class test {
  12. public static void main(String[] args) {
  13. int a[]={0,1,2,3,4,5,6,7,8,9};
  14.      for(int i :a){
  15.       System.out.print(i+" ");
  16.      }
  17. }
  18. }
  19. 在上面这个例子 增强型的for循环 和普通for循环一样
  20. 增强型的for循环 优点主要体现在集合中,随便举个例子
  21. 比如对 set 的遍历
  22. 一般是迭代遍历:
  23. Set<String> set = new HashSet<String>();
  24. Iterator<String> it = set.iterator();
  25. while (it.hasNext()) {
  26. String str = it.next();
  27. System.out.println(str);
  28. }
  29. for循环遍历:
  30. for (String str : set) {
  31. System.out.println(str);
  32. }
  33. 是不是简单些?

  34. 优点还体现在泛型 假如 set中存放的是Object

  35. Set<Object> set = new HashSet<Object>();
  36. for循环遍历:
  37. for (Object obj: set) {
  38. if(obj instanceof Integer){
  39. int aa= (Integer)obj;
  40. }else if(obj instanceof String){
  41. String aa = (String)obj
  42. }
  43. ........
  44. }
  45. 如果你用Iterator遍历,那就晕了
  46. map list 也一样

  47. 唯一的缺点就是 在遍历 集合过程中,不能对集合本身进行操作
  48. for (String str : set) {
  49. set.remove(str);//错误!
  50. }</b>
复制代码


(我连夜做的)但字数太多就不让发表了就分开发了


点评

你可以发表在一楼呀!!!  发表于 2014-2-28 10:29

评分

参与人数 1黑马币 +5 收起 理由
何伟超 + 5

查看全部评分

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马