Converting an int array to a String array

前端 未结 14 1344
予麋鹿
予麋鹿 2020-12-01 14:21

So I have this \"list\" of ints. It could be a Vector, int[], List, whatever.

My goal though is to sort the

相关标签:
14条回答
  • 2020-12-01 14:46

    Why don't you simply cast those values to String within the original for loop, creating a String array rather than an int array? Assuming that you're gathering your initial integer from a starting point and adding to it on each for loop iteration, the following is a simple methodology to create a String array rather than an int array. If you need both int and String arrays with the same values in them, create them both in the same for loop and be done with it.

    yourInt = someNumber;
    
    for (int a = 0; a < aLimit; a ++) {
    
    String stringName = String.valueOf(yourInt);
    StringArrayName[a] = stringName;
    yourInt ++;
    
    }
    

    Or, if you need both:

    yourInt = someNumber;
    
    for (int a = 0; a < aLimit; a ++) {
    
    String stringName = String.valueOf(yourInt);
    StringArrayName[a] = stringName;
    intArrayName[a] = yourInt;
    yourInt ++;
    
    }
    

    I agree with everyone else. For loops are easy to construct, require almost no overhead to run, and are easy to follow when reading code. Elegance in simplicity!

    0 讨论(0)
  • 2020-12-01 14:51

    Can I use a while loop instead?

    @Test
    public void test() {
        int[] nums = {5,1,2,11,3};
    
        Arrays.sort(nums);
    
        String[] stringNums = new String[nums.length];
        int i = 0;
        while (i < nums.length) {
            stringNums[i] = String.valueOf(nums[i++]);
        }
    
        Assert.assertArrayEquals(new String[]{"1","2","3","5","11"}, stringNums);
    }
    

    Using JUnit assertions.

    Sorry, I'm being flippant. But saying you can't use a for loop is daft - you've got to iterate over the list somehow. If you're going to call a library method to sort it for you (cf Collections.sort()) - that will be looping somehow over the elements.

    0 讨论(0)
  • 2020-12-01 14:51

    Java 8 way, no loops at all:

     // Given
    int[] array         = {-5, 8, 3, 10, 25};
    // When
    String[] actual = Arrays.stream(array)
            .sorted()
            .mapToObj(String::valueOf)
            .toArray(String[]::new);
    // Then
    String[] expected = {"-5", "3", "8", "10", "25"};
    
    assertArrayEquals(expected, actual);
    
    0 讨论(0)
  • 2020-12-01 14:52

    Arrays.sort(nums); var stringArray = (nums.toString()).split(',').map(String);

    0 讨论(0)
  • 2020-12-01 14:53
    int[] nums = {5,1,2,11,3}; //List or Vector
    Arrays.sort(nums); //Collections.sort() for List,Vector
    String a=Arrays.toString(nums); //toString the List or Vector
    String ar[]=a.substring(1,a.length()-1).split(", ");
    System.out.println(Arrays.toString(ar));
    

    UPDATE:

    A shorter version:

    int[] nums = {-5,1,2,11,3};
    Arrays.sort(nums);
    String[] a=Arrays.toString(nums).split("[\\[\\]]")[1].split(", "); 
    System.out.println(Arrays.toString(a));  
    
    0 讨论(0)
  • You can use Collections.sort() and then iterate over the list and collectString.valueOf() each element.

    http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29

    For a Vector, you would first get a List with Collections.list(Enumeration e).

    For an array, you would use Arrays.sort() instead of Collections.sort().

    http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort%28int%5b%5d%29

    0 讨论(0)
提交回复
热议问题