Topic: 求助!怎样将string放进数组 ?

  Print this page

1.求助!怎样将string放进数组 ? Copy to clipboard
Posted by: 欲空大师
Posted on: 2007-04-27 21:23

例如:String str ="0001101";
有一个数组 byte result1[] = new byte[32],要让 result1[0]=1, result1[1]=0, result1[2]=1,即就是string里的每一位在数组里占一个元素,求高手指教!
API 我看不懂

2.Re:求助!怎样将string放进数组 ? [Re: 欲空大师] Copy to clipboard
Posted by: 133xialiang
Posted on: 2007-04-28 17:30

是把str倒序放入result1数组里吗

3.Re:求助!怎样将string放进数组 ? [Re: 欲空大师] Copy to clipboard
Posted by: xuxiaolei
Posted on: 2007-04-29 16:35

String s = "0001101";
char[] array = new char[s.length()];
s.getChars(0, s.length(), array, 0);
for(int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}    

按照楼主的意思,应该是这样写,我也不是高手,如果没有看API或者基础书的耐性,别搞JAVA了,希望楼主沉下心来,不要浮躁

4.Re:求助!怎样将string放进数组 ? [Re: 欲空大师] Copy to clipboard
Posted by: hollyman
Posted on: 2007-06-13 18:23


String str ="0001101";
byte result[] = new byte[str.length()];
for(int i=0;i<str.length();i++){
result[i]=(byte)str.charAt(str.length()-1-i);
}

5.Re:求助!怎样将string放进数组 ? [Re: 欲空大师] Copy to clipboard
Posted by: tuntun_wj
Posted on: 2007-09-09 11:16

String s = "0001101";
char[] array = new char[s.length()];
s.getChars(0, s.length(), array, 0);
for(int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}

6.Re:求助!怎样将string放进数组 ? [Re: 欲空大师] Copy to clipboard
Posted by: hubmygirl
Posted on: 2007-09-09 17:26

package review3;

public class byteArray {
  static char[] array;
  public static void main(String[] args) {
    String s = "0001101";
    array = new char[s.length()];
    s.getChars(0, s.length(), array, 0);

    swap();
    for (int i = 0; i < array.length; i++) {
      System.out.println(array[i]);
    }
  }

  private static void swap() {
    char temp;
    for (int i = 0; i < array.length / 2; i++) {
      temp = array[i];
      array[i] = array[array.length - i - 1];
      array[array.length - i - 1] = temp;
    }

  }

}

7.Re:求助!怎样将string放进数组 ? [Re: 欲空大师] Copy to clipboard
Posted by: long_5281
Posted on: 2007-11-08 14:51

public static void main(String[] args){
StringBuffer sb = new StringBuffer("0001101"); //定义字符串
String str = sb.reverse().toString();//将字符串反向
byte[] resultStr = str.getBytes();
for(byte b:resultStr){
System.out.println((char)b);
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923