Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int---->integer array

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to convert a given integer to an integer array.

eg int=1234 ----> int[] ={1,2,3,4}

 
Janeice DelVecchio
Saloon Keeper
Posts: 1808
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe change it to a string, then a character array, then to an Integer array?

There must be a better way....
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divide and conquer.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with dividing is that you have to loop twice; once to get to know the array size, then another to fill it. Even if you use a List you still need to loop twice; the second loop to convert a List<Integer> to an int[].
Then again, converting to String and then converting each separate character to a digit (using Character.digit) also is two loops; the int to String also has a loop.

I don't think you can get past a double loop. That leaves an option between ease (int -> String, char -> int) and efficiency (dividing). Well, if dividing is more efficient; I'd have to test first.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic