-3

Given an ArrayList of ArrayList of Bytes like this :

 ArrayList<ArrayList<Byte> > shares = new ArrayList<ArrayList<Byte> >(); 

I want to convert this to an array of String which is defined like this :

String[] binary=new String[shares.size()];

How to do it.Please help

How to do reverse of it also.Means how to convert this string back to arraylist.

  • Check this answer hope it will help you http://stackoverflow.com/questions/9572795/convert-list-to-array-in-java – Sergey Pekar Mar 17 '14 at 10:36
  • Most basic approach would be iterate over outer list and for each outer iteration iterate on the inner list and put it in the array. Time complexity --> **O(N^2)** – Aniket Thakur Mar 17 '14 at 10:37

3 Answers3

0

Check toArray method of ArrayList Also you can find simmilar question here

Community
  • 1
  • 1
Sergey Pekar
  • 7,940
  • 7
  • 42
  • 51
0

Iterate over the outer list and convert every entry to a string.

e.g.

ArrayList<String> result = new ArrayList<>(shares.size());
for(ArrayList<Byte> bytes: shares) {
   result.add(bytes.toString());
}

If you want the result as a string array you can do it this way:

String[] result = new String[shares.size()];
for(int i = 0; i < result.length; ++i) {
   ArrayList<Byte> bytes = shares.get(i);
   result[i] = bytes.toString();
}

If the bytes actually represent a string (you want the characters and not the bytes as numbers printed) then you would have to convert the inner list to an array (see other answers) and instantiate a string with it. If you want the encoding stuff see Java - String getBytes()

morpheus05
  • 4,461
  • 2
  • 26
  • 46
0

Have a look at this will help-

  public static void main(String[] args) {
    ArrayList<ArrayList<Byte>> shares = new ArrayList<ArrayList<Byte>>(0);
    ArrayList<Byte> b = new ArrayList<Byte>();
    b.add(new Byte("0"));
    b.add(new Byte("1"));

    shares.add(b);

    Byte[] b1 = new Byte[shares.size()];
    b1 = shares.get(0).toArray(b1);
    System.out.println(Arrays.toString(b1));

}
Shekhar Khairnar
  • 2,453
  • 3
  • 22
  • 43