Konwertuj liczbę całkowitą na tablicę znaków: java

Jaki jest najlepszy sposób na konwersję liczby całkowitej do tablicy znaków?

Wejść: 1234

Wyjście: {1,2,3,4}

Mając na uwadze ogrom języka Java jaki będzie najlepszy i najskuteczniejszy sposób?

Author: Erfan, 2012-08-30

7 answers

int i = 1234;
char[] chars = ("" + i).toCharArray();
 31
Author: AlexR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-30 08:28:38

Możesz spróbować czegoś takiego:

String.valueOf(1234).toCharArray();
 26
Author: JesperE,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-30 08:29:13

Spróbuj tego...

int value = 1234;
char [] chars = String.valueOf(value).toCharArray();
 10
Author: SiB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-30 08:30:46

Możesz przekonwertować tę liczbę całkowitą na łańcuch znaków, a następnie przekształcić ten łańcuch na znak arary: -

int i = 1234;
String s = Integer.toString(i);
Char ch[] = s.toCharArray();

/*ch[0]=1,ch[1]=2,ch[2]=3,ch[3]=4*/
 3
Author: Vinit Raj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-04 08:59:46

Spowoduje konwersję tablicy int na tablicę 2 znaków. Jeśli próbujesz uzyskać minimalną ilość znaków, spróbuj tego.

//convert int to char array

int valIn = 111112222;

ByteBuffer bb1 = ByteBuffer.allocate(4);
bb1.putInt(valIn); 

char [] charArr = new char[4];
charArr[0] = bb1.getChar(0);
charArr[1] = bb1.getChar(2);

//convert char array to int

ByteBuffer bb2 = ByteBuffer.allocate(8);
bb2.putChar(0,charArr[0]);
bb2.putChar(2,charArr[1]);

long valOut = bb2.getInt(0);
 0
Author: live-love,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-10-02 00:55:37

Zadano mi to pytanie w wywiadzie google. W przypadku pytań w wywiadach użyj modułu i dywizji. Oto odpowiedź

List<Integer> digits = new ArrayList<>();
//main logic using devide and module
for (; num != 0; num /= 10)
    digits.add(num % 10);

//declare an array
int[] arr = new int[digits.size()];
//fill in the array
for(int i = 0; i < digits.size(); i++) {
    arr[i] = digits.get(i);
}
//reverse it.
ArrayUtils.reverse(arr);
 -1
Author: harshad_y,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-18 16:20:06

Powiedz, że masz tablicę ints i inną metodę, która konwertuje te ints na litery, jak w przypadku programu zmieniającego stopnie liczbowe na stopnie literowe, zrobiłbyś to...

public char[] allGradesToLetters()
   {
      char[] array = new char[grades.length];

      for(int i = 0; i < grades.length; i++)
      {
         array[i] = getLetter(grades[i]);
      }

      return array;
   }
 -1
Author: CompoterHenius,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-07 19:01:44