简体   繁体   中英

Converting int array to String and returning (getArrayString)

I have to make a getArrayString(int[] array, char separator) method which return a string where each array entry (except the last one) is followed by the specified separator.

I know the structor of the arrays methods but in this one first I have to fined a formula for returning array except the last one and second convert my int[] array to String and return it with separator.

public static getArrayString(int[] array, char separator) {
    if(array == null ||array.length == 0) {
        return null;
    }
    int size =0;
    for(int i =0; i < array.length; i++) {

        size++;
    }
    int[] newArray2 = new int[size];
    for(int i= 0, position = 0; i < array.length; i++) {
        newArray2[position]=i;
        position++;
        System.out.print(i+',');
    }
    //  String StArray = Arrays.toString(newArray2);
    //  return StArray + separator;
    //  newArray2 = convertStringArrayToString(StArray,separator);
    //  return StArray;
    //  
    //return Arrays.toString(newArray2)+ separator;
}
// private static String convertStringArrayToString(String[] newArray,char separator) {
//   StringBuilder sb = new StringBuilder();
//   for(String StArray : newArray);
//   return sb.substring(0,sb.length()-1);
// }

The comments are ideas of converting to String and returning it!

Joining int[] to a String

Edit: I just spotted you have an int[] not a String[]. I would probably use the Streams approach:

String result = Arrays.stream(array)
    .mapToObj(String::valueOf)
    .collect(Collectors.joining(separator));

If you really want to use a for-loop, you should use a StringJoiner :

public static getArrayString(int[] array, char separator) {
    if(array == null || array.length == 0) return "";

    String separatorString = String.valueOf(separator);
    StringJoiner sj = new StringJoiner(separatorString);

    for(int element : array) {
        sj.add(String.valueOf(element));
    }

    return sj.toString();
}

Joining String[] to a String

The phrasing of your question makes me think you're studying this for a class, but if you're not, just use the in-built join function:

// join takes Iterable<? extends CharSequence> or vararg
String.join(separator, myArray)
String.join(separator, myList)
String.join(separator, charSeq1, charSeq2, ..., charSeqN)

If you need prefix/suffix, you can use a StringJoiner :

StringJoiner sj = new StringJoiner(separator, prefix, suffix);
String result = sj.add(s1).add(s2).add(s3).toString();

...which is also the basis of the Stream Collectors.joining:

String result = Arrays.stream(array)
    .collect(Collectors.joining(separator));

String result = Arrays.stream(array)
    .collect(Collectors.joining(separator, prefix, suffix));
public static String getArrayString(int[] array, char separator) {
    if(array == null || array.length == 0) {
        return null;
    }

    String str = "";
    for(int i = 0; i < array.length-1; i++) {
        str += "" + array[i]+ separator;
    }

    // add the last one
    str += array[array.length - 1];
    return str;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM