java手记---数组,作用域,引用

 boolean [] a;
 a = new boolean [] {true,false};
 System.out.println(a[0]+" "+a[1]);    //this is right.
 System.out.println(a[0]+a[1]);         //this is wrong. Because two boolean can't add .
 System.out.println(a[0],a[1]);           //this is also wrong. Because the function can't take two paraments               take two paraments.
java 的数组:
  ArrayList c;
  c = new ArrayList();
  for(int i = 0;i<26;i++)
  c.add("hello"+i);
  System.out.println(c);
可以在程序过程中输入任意多个数据放入这种数组。
  Iterator a = c.iterator();//这里是获得c的迭代器。
  while(a.hasNext())这里判断下一个是否存在。
  System.out.println(a.next());
/这里不要产生误解:认为第一个打印不出来。这里可以看出迭代器初始值是指向第一个元素,
/打印时只是先打印了元素再向后指。
/不过要说明的是java中没有指针这个概念,不过我学c/c++多年很自然的想用这个概念。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Slope:
  for(int i = 0;i < 10;i++)
  System.out.println(i);
  for(int i = 0;i < 10;i++)
  System.out.println(i*10);
这样写并没有出错,这与c++不同。代表在for循环以外i已经被销掉。不能再使用i这个变量
  int i = 3;
  {
   char i = 'c';//这里将出错,与c++不同。c++中将在第一个输出点输出c
  System.out.println(i);   
  }
  System.out.println(i); 
~~~~~~~~~~~~~~~~~~~~~~~~~~
  int i = 3;
  {
   char q = 'c';
  System.out.println(q);   
  }
  System.out.println(i);
  System.out.println(q);///这里也会编译失败,原因是q已经超出作用范围。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
reference:

///在声明数组或对象时,创建的是一个引用。无论声明什么样的数组,引用都是32位宽。
int [] ages;
int [] otherAges;
otherAges = new int [5];
ages = otherAges;  //认为把otherAges的引用赋给ages
如果otherAges[2] = 2;
则ages[2] == 2;
//函数不用声明。
//传入到函数中的数组被永久的改变了。
int [] b = new int []{1,2,3,5,7};  // this is right.
int [] b = new int [5]{1,2,3,5,7}; //this is wrong.
/
 static int[] creat(int a)                         ///返回一个数组。
 {
  int[] array = new int [a];
  ..................................
  return array;                       ///只写数组名。
 }
这里说明一下指针和引用的两个概念:
指针是指向地址的,而引用不是.
指针指向地址带来的麻烦就是指针可以乱指,而且有时程序中根本无法判断它是否已经越界.更可能指向系统区并修改里面的数据.....后果可想而知.
而引用是一个对象的另一个名字,
Object a = new Object();
Object b = a;
这样b成了a的引用.b其实是a的另一种叫法.所以改变了b就相当于改变了a..
因为java中没有指针这个东西,所以比c/c++使用起来安全的多.错误也很容易被编译器发现.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值