字符串 数组 分割成 等量的 3份(大虾帮助) 。。

geniusfzq 2010-04-13 01:59:52
String number = "000000";
String id = "310141597210452232";
all = id + number;
byte[] messages = new byte[24];
messages = all.getBytes();
for (int i = 0; i < 24; i++) {
System.out.print(messages[i]);

打印結果是 514948495249535755504948525350505150484848484848

我想要的結果是

messages[i]/3 的结果

也就是 可以分成3个数组 arr1
arr2
arr3
打印的结果是
5149484952495357
5550494852535050
5150484848484848
...全文
198 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
keeya0416 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 geniusfzq 的回复:]
引用 13 楼 keeya0416 的回复:
对 8 楼写错了 自己都忘了 呵呵

Java code

String number = "000000";
String id = "310141597210452232";
String all = id + number;
byte[] messages = new byte[all.length()];
……


对不……
[/Quote]
数组都分配好了
具体要打印成什么样子的在里边处理下就可以了
LZ自己改下吧 忙ing。
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
晕,你到底要打印还是要传输?那是一个3*8结构的二维数组,打印二维数组会吧?
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(strArray[i][j]);
}
System.out.println();
}
geniusfzq 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 pomelover 的回复:]
错了,是strArray[0],strArray[1],strArray[2]是你想要的结果
[/Quote]

System.out.println(strArray[0]);
System.out.println(strArray[1]);
System.out.println(strArray[2]);

打印的结果是
[B@192d342
[B@6b97fd
[B@1c78e57
这个是什么问题呢 望解答!!~
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
错了,是strArray[0],strArray[1],strArray[2]是你想要的结果
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
我崩溃了,楼主怎么回事,他不是加new String(arr1)了嘛!
public static void main(String[] args) {
String number = "000000";
String id = "310141597210452232";
String all = id + number;
byte[] messages = all.getBytes();
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i]);
}
byte[][] strArray = new byte[3][messages.length / 3];
if (messages.length % 3 == 0) {
for (int i = 0; i < 3; i++) {
strArray[i] = new String(messages, i * messages.length / 3, 8)
.getBytes();
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(strArray[i][j]);
}
System.out.println();
}
}

上面代码中strArray[1],strArray[2],strArray[3]是你想要的结果
geniusfzq 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 keeya0416 的回复:]
对 8 楼写错了 自己都忘了 呵呵

Java code

String number = "000000";
String id = "310141597210452232";
String all = id + number;
byte[] messages = new byte[all.length()];
……
[/Quote]

对不起 我要求的 打印结果是 以下的这些
而你的就是 前面的 String 数值!~·1
有点问题!~~
arr1[8] = 5149484952495357
arr1[8] = 5550494852535050
arr3[8] = 5150484848484848

主要是我要传送到网络去的!~!~多谢
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
楼上也是对的,事实上调用all.getBytes()之前所有对messages分配空间的代码都是没有意义的。
keeya0416 2010-04-13
  • 打赏
  • 举报
回复
对 8 楼写错了 自己都忘了 呵呵

String number = "000000";
String id = "310141597210452232";
String all = id + number;
byte[] messages = new byte[all.length()];
messages = all.getBytes();
byte[] arr1 = new byte[messages.length / 3];
byte[] arr2 = new byte[messages.length / 3];
byte[] arr3 = new byte[messages.length / 3];
int temp = 0;
while(temp < messages.length / 3 ){
arr1[temp] = messages[temp++];
}
while(temp < (messages.length / 3) * 2){
arr2[temp - messages.length / 3] = messages[temp++];
}
while(temp < messages.length){
arr3[temp - (messages.length / 3) * 2] = messages[temp++];
}
System.out.println(new String(arr1));
System.out.println(new String(arr2));
System.out.println(new String(arr3));

这个可以了
geniusfzq 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 pomelover 的回复:]
8楼的肯定会越界,看看二楼是不是你想要的?怎么格式化代码?
[/Quote]

可以这样分成3个数组 arr1[8] = 5149484952495357
arr1[8] = 5550494852535050
arr3[8] = 5150484848484848

主要是我要传送到网络去的!~!~多谢
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
public static void main(String[] args) {
String number = "000000";
String id = "310141597210452232";
String all = id + number;
byte[] messages = all.getBytes();
for (int i = 0; i < messages.length; i++) {
System.out.println(messages[i]);
}
byte[][] strArray = new byte[3][messages.length / 3];
if (messages.length % 3 == 0) {
for (int i = 0; i < 3; i++) {
strArray[i] = new String(messages, i * messages.length / 3, 8)
.getBytes();
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(strArray[i][j]);
}
System.out.println();
}
}
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
8楼的肯定会越界,看看二楼是不是你想要的?怎么格式化代码?
geniusfzq 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 geniusfzq 的回复:]
引用 1 楼 mehere8 的回复:
你是要赋值给arr1,2,3 还是打印?


for (int i = 0; i < 24; i++) {
if (i%8==0) System.out.println();
System.out.print(messages[i]);
}


大虾 我是要赋值 给 ARR1 ,2 , 3
谢谢 请教教我吧
[/Quote]


大虾们 赋值到 arr1 ,arr2 ,arr3
有点问题 。请帮忙 谢谢!~~
keeya0416 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 geniusfzq 的回复:]
大虾 我是要赋值 给 ARR1 ,2 , 3
谢谢 请教教我吧
[/Quote]
byte[] messages = new byte[24];
messages = all.getBytes();
byte[] arr1 = new byte[8];arr2 = new byte[8];arr3 = new byte[8];
int temp = 0;
while(temp < 8){
arr1[temp] = messages[temp++];
}
while(temp < 16){
arr2[temp] = messages[temp++];
}
while(temp < 24){
arr3[temp] = messages[temp++];
}
以上是硬编码 不建议程序中这么使用
keeya0416 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 keeya0416 的回复:]
引用 1 楼 mehere8 的回复:
你是要赋值给arr1,2,3 还是打印?


for (int i = 0; i < 24; i++) {
if (i%8==0) System.out.println();
System.out.print(messages[i]);
}

这个是不是要改成 if(i%8 == 7)
[/Quote]
ya不好意思 搞错了 1楼是对的
keeya0416 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mehere8 的回复:]
你是要赋值给arr1,2,3 还是打印?


for (int i = 0; i < 24; i++) {
if (i%8==0) System.out.println();
System.out.print(messages[i]);
}
[/Quote]
这个是不是要改成 if(i%8 == 7)
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
哦,对了,顺便问一句,代码粘上来怎么让他按代码的格式对齐?标签是什么?
geniusfzq 2010-04-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 mehere8 的回复:]
你是要赋值给arr1,2,3 还是打印?


for (int i = 0; i < 24; i++) {
if (i%8==0) System.out.println();
System.out.print(messages[i]);
}
[/Quote]

大虾 我是要赋值 给 ARR1 ,2 , 3
谢谢 请教教我吧
zoeg 2010-04-13
  • 打赏
  • 举报
回复
无语,看题目看得头疼!!!!!!!!!!!!!!
James.Ji 2010-04-13
  • 打赏
  • 举报
回复
你这个问题本来就是错误的,你的理想是把all获得的字节数组放在message中,但是实际的情况是all.getBytes()会重新分配内存,原来message的内存被抛弃了,所以应该是
for (int i = 0; i < messages.length; i++) {
System.out.print(messages[i]);
}
分成三个的方法是
byte[][] strArray = new byte[3][messages.length / 3];
if (messages.length % 3 == 0) {
for (int i = 0; i < 3; i++) {
strArray[i] = new String(messages, i * messages.length / 3, 8)
.getBytes();
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 8; j++) {
System.out.print(strArray[i][j]);
}
System.out.println();
}
mehere8 2010-04-13
  • 打赏
  • 举报
回复
你是要赋值给arr1,2,3 还是打印?


for (int i = 0; i < 24; i++) {
if (i%8==0) System.out.println();
System.out.print(messages[i]);
}

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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