There is a function that accepts CharSequence [] (this is AlertBuilder.setItems on Android), you need to pass a char [] to it. How to bring or translate one into another?

    5 answers 5

    Generally speaking, the String is also a CharSequence ; accordingly, the method can be:

     // char[] array = ... CharSequence sequence = new String(array); 

    As an alternative without additional copying:

     CharSequence sequence = java.nio.CharBuffer.wrap(array); 

      Your way is very complicated. Why so confusing? Why just

       char[] array = "My test".toCharArray(); // исходный массив символов CharSequence[] seq = new CharSequence[] { new String(array) }; 

      Or (without copying charms)

       char[] array = "My test".toCharArray(); // исходный массив символов CharSequence[] seq = new CharSequence[] { CharBuffer.wrap(array) }; 

        Found a way

         List<char[]> l = Arrays.asList(from); CharSequence[] to = l.toArray(new CharSequence[l.size()]); 

          I do not know how about the effectiveness, but it works (and I understand how)

            CharSequence [] to = new CharSequence [from.length]; for (int i = 0; i < to.length; i++) to[i] = ""+from[i]; 

            It is more logical to use String as an intermediate type (and more obvious).

             CharSequence cs; char[] ch="test".toCharArray(); cs=(CharSequence) ch.toString(); 

            • ch.toString () - returns the address of the array in memory plus the type signature. - cy6erGn0m