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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 贾存双 中级黑马   /  2012-7-17 05:17  /  1646 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 贾存双 于 2012-7-17 07:39 编辑

import java.io.* ;
public class Demo{
public static void main(String args[])throws Exception{
  File f = new File("e:" + File.separator + "io" + File.separator + "RandomAccessFile" + File.separator + "Random.txt") ;
  RandomAccessFile raf = new RandomAccessFile(f,"rw") ;
  String name = null ;
  int age = 0 ;
  name =  "Jrain   " ;
  age = 22 ;
  raf.writeBytes(name) ;
  raf.writeInt(age) ;
  name =  "xiaojian" ;
  age = 20 ;
  raf.writeBytes(name) ;
  raf.writeInt(age) ;
  raf.close() ;
  RandomAccessFile rf = new RandomAccessFile(f,"r") ;
  byte b[] = new byte[8] ;
  rf.skipBytes(12) ;
  for(int i = 0;i<b.length;i++){
   b = rf.readByte() ;
  }
/*for(byte i:b){
   i = rf.readByte() ;
  }*/                                                 如果把for循环改为foreach的形式,则byte数组读不到信息,是怎么回事???
int a = rf.readInt() ;
  String str = new String(b) ;
  System.out.println("读取到的姓名:" + str + "; 年龄:" + a) ;
}
}
这是普通的for循环打印出来的结果:


这是用foreach打印出来的结果:

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

2 个回复

倒序浏览
本帖最后由 张_涛 于 2012-7-17 06:18 编辑
  1. import java.io.*;

  2. public class Demo1 {
  3.         public static void main(String args[]) throws Exception {

  4.                 File f = new File("Random.txt");
  5.                 RandomAccessFile raf = new RandomAccessFile(f, "rw");
  6.                 String name = null;
  7.                 int age = 0;
  8.                 name = "Jrain   ";
  9.                 age = 22;
  10.                 raf.writeBytes(name);
  11.                 raf.writeInt(age);
  12.                 name = "xiaojian";
  13.                 age = 20;
  14.                 raf.writeBytes(name);
  15.                 raf.writeInt(age);
  16.                 raf.close();
  17.                 RandomAccessFile rf = new RandomAccessFile(f, "r");
  18.                 byte b[] = new byte[8];
  19.                 rf.skipBytes(12);
  20. //                for (int i = 0; i < b.length; i++) {
  21. //                        b[i] = rf.readByte();
  22. //                        System.out.println(b[i]);
  23. //                }
  24.                 for (byte i : b) {
  25.                         i = rf.readByte();
  26.                         System.out.println(i);
  27.                 }
  28.                 //以下三行是我增加的内容。
  29.                 for (int i = 0; i < b.length; i++) {
  30.                         System.out.println(b[i]);
  31.                 }
  32.                 int a = rf.readInt();
  33.                 String str = new String(b);
  34.                 System.out.println("读取到的姓名:" + str + "; 年龄:" + a);
  35.         }
  36. }
  37. 运行结果:
  38. 120
  39. 105
  40. 97
  41. 111
  42. 106
  43. 105
  44. 97
  45. 110
  46. 0
  47. 0
  48. 0
  49. 0
  50. 0
  51. 0
  52. 0
  53. 0
  54. 读取到的姓名:
复制代码
这样,可以看出来,在foreach循环中对循环变量赋值并没有影响到原数组。原因如下:
使用foreach循环迭代数组、集合时,循环计数器只是保存了当前正在遍历的数组元素、集合元素的值,并不是数组元素、集合元素本身,而只是一个复制副本。因此不要对foreach循环的循环计数器(即循环变量)进行赋值,虽然语法允许,但是没有太大的意义,而且还可能出现错误。
另可以看看活动贴,了解更多:http://bbs.itheima.com/thread-19543-1-1.html

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
谢谢 受教了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马