2

I would like to re-format a String array based on condition. Say, the array

A = ["samsung", "chargers", "fast", "charging", "rapid", "high"]

int index = 1

Which means I will adjoin the items till index 1 with space and format the array. So, finally, it will be,

A = ["samsung chargers", "fast", "charging", "rapid", "high"]

For the index = 2, the output should be,

A = ["samsung chargers fast", "charging", "rapid", "high"]

I write the code that works, I try to find more concise (but not low performance) way.

StringBuilder builder = null;

..........

int fCount = ...

// format the array to match the string
// values = ["samsung", "chargers", "fast", "charging", "rapid", "high"]

builder = new StringBuilder();
String formated = "";

for (int i = 0; i <= fCount; i++) {
    builder.append(values[i]).append(" ");
}

formated = builder.toString().trim();

String[] fVaues = new String[values.length - fCount];

fVaues[0] = formated;

for (int i = 1; i < fVaues.length; i++) {
    fVaues[i] = values[i+1];
}

What is the simple way to accomplish it?

6
  • 4
    writing code that does so Commented Feb 28, 2019 at 11:05
  • 2
    It's literally a simple for loop, we won't provide you with homework when you show no attempt to solve it yourself. Commented Feb 28, 2019 at 11:08
  • 1
    As a 1k+ reputation you shouldn't be ask home work question. Hope you aware of this site? Commented Feb 28, 2019 at 11:09
  • @soorapadman I am sorry if I misrepresent myself. I have code that works.... I simply thought there might be a better way to implement it. Commented Feb 28, 2019 at 11:14
  • 1
    if it is to find a better way, maybe you should post this on the review site, instead of on SO Commented Feb 28, 2019 at 11:16

3 Answers 3

4

This method does the same thing:

static String[] joinUntil(String[] original, int until) {
    return Stream.concat(
                Stream.of(String.join(" ", Arrays.copyOf(original, until))),
                Arrays.stream(Arrays.copyOfRange(original, until, original.length))
            ).toArray(String[]::new);
}
Sign up to request clarification or add additional context in comments.

Comments

3
private static List<String> reFormat(List<String> lst, int index){
    String joined = String.join(" ", lst.subList(0, index + 1));
    List<String> res = new ArrayList<String>();
    res.add(joined);
    res.addAll(lst.subList(index + 1, lst.size()));
    return res;
}

Comments

2

You could just loop over it, adding the Strings to a second array:

String[] b = new String[a.length - index];
String tmp = a[0];

for(int i = 1; i < a.length; i++) {
    if(i <= index) {
        tmp += " " + a[i];

        if(i == index) {
            b[i - index] = tmp;
        }
    }
    else {
        b[i - index] = a[i];
    }
}

1 Comment

Generally use the StringBuilder like builder = builder.append(values[j]).append(" "); In the decompiled class, the line tmp += " " + a[i]; will be new StringBuilder(" ").append(a[i]). This means in each iteration you creates a new obj which doesn't provides good performance. If its a one-liner, we can safely use them StringBuilder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.