Converting an int array to a String array

前端 未结 14 1430
予麋鹿
予麋鹿 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!

提交回复
热议问题