简直要哭了。。。。。遇到的最最最最最诡异的java问题!!

b522485449 2014-04-24 08:06:32
这次真是跪了。。。。原来基本都是在百度知道上问,,,这次实在不行了来找csdn上的各位。。。
谁要是把它解决了小弟真是跪谢了!!


问题是这样的,输入流的read方法居然莫名其妙的改变了我其中一个变量的值!!而我这个变量明明和它没关系啊!!
还别不信各位大神,上代码


byte[] bytes=new byte[]{12,34,56,78,12,34,56,78}; //用作输入流的byte组

InputStream is=new ByteArrayInputStream(bytes); //将byte组封装成输入流

int tmp;
byte[] bytesRes = new byte[2]; //每次读取两个字节
byte[] Data = new byte[0]; //用来储存总数据的Data,出现状况的变量就是它

tmp =0;
while (true) {
System.out.println(" \n Data是 \n ");
for(byte b:Data){System.out.print(" "+b);} //打印Data 1号

tmp = is.read(bytesRes); //读入数据

System.out.println(" \n read后Data是 \n ");
for(byte b:Data){System.out.print(" "+b);} //打印Data 2号

if(tmp==-1){break;} //读入完成就退出循环
Data=hebing(Data, bytesRes, tmp); // 函数hebing是把bytesRes前tmp个字节加入Data中返回
}

就是这样了,在程序运行时,Data1号居然和Data2号打印出了不一样的数据??!!太匪夷所思了!!
Data是

12 34
read后Data是

56 78

就是这样的。。。我已经实在无语凝噎了。。。。。
...全文
167 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
b522485449 2014-04-24
  • 打赏
  • 举报
回复
哦对!是因为数组是引用量来着。。。。。卧槽。。。我真是。。。傻逼。。。 原来就没搞得怎么清楚。。哎。。也就是说引用量的=是将指向的地址相同了是吧。
b522485449 2014-04-24
  • 打赏
  • 举报
回复
引用 3 楼 foxpeter 的回复:
System.out.println(bytesRes); tmp = is.read(bytesRes); System.out.println(Data); System.out.print("read后Data是"); 问题应该在hebing函数里 这个函数之后Data和bytesRes是同一个对象了
求指教!!!!!!!!! 为什么byte1=byte2了之后这两个就成一个变量了啊 我居然连这种基本功都没打扎实。。。太惭愧了。。。 跪谢!!!!!!
byourfan 2014-04-24
  • 打赏
  • 举报
回复
中间bytesRes和Data同一个对象了。用数组作为参数/返回值的时候要注意有时候需要把数组复制一份。
foxpeter 2014-04-24
  • 打赏
  • 举报
回复
System.out.println(bytesRes); tmp = is.read(bytesRes); System.out.println(Data); System.out.print("read后Data是"); 问题应该在hebing函数里 这个函数之后Data和bytesRes是同一个对象了
b522485449 2014-04-24
  • 打赏
  • 举报
回复
来人啊!!!!!!!!!
b522485449 2014-04-24
  • 打赏
  • 举报
回复
至于那个合并函数,意义不大,不过我把全部代码发到这里,各位高手可以自己测试。。。 package com.example.http; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; public class main222 { public static byte[] hebing(byte[] bytes1, byte[] bytes2, int n) { //==== int n1 = bytes1.length; if (n1 == 0) { byte[] bytes3=new byte[n]; if(bytes2.length>n){ System.arraycopy(bytes2, 0, bytes3, 0, n); return bytes3; } return bytes2; } int n2 = bytes2.length; if (n2 == 0) { return bytes1; } int n3 = n1 + n;// 新数组的长度 byte[] bytes3 = new byte[n3]; ArrayList<Byte> arr = new ArrayList(); for (byte b : bytes1) { arr.add(b); // System.out.print(" "+b); } for (byte b : bytes2) { arr.add(b); } for (int i = 0; i <= n3 - 1; i++) { bytes3[i] = arr.get(i); } return bytes3; } public static void main(String[] args) throws IOException, InterruptedException { // TODO 自动生成的方法存根 byte[] bytes=new byte[]{12,34,56,78,12,34,56,78}; InputStream is=new ByteArrayInputStream(bytes); int tmp; byte[] bytesRes = new byte[2]; byte[] Data = new byte[0]; tmp =0; while (true) { System.out.println(" \n Data是 \n "); for(byte b:Data){System.out.print(" "+b);} tmp = is.read(bytesRes); System.out.println(" \n read后Data是 \n "); for(byte b:Data){System.out.print(" "+b);} if(tmp==-1){break;} Data=hebing(Data, bytesRes, tmp); } System.out.println(); System.out.println(12333333); for(byte b:Data){System.out.print( " "+b);} } }

50,545

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧