Reformater un tableau chaîne

Chaklad est le général Ashfaq Arafa:

Je voudrais reformater un tableau chaîne en fonction des conditions. Dites, le tableau

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

int index = 1

Ce qui signifie que je raccordent les éléments jusqu'à l'index 1 avec l'espace et le format du tableau. Alors, enfin, il sera,

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

Pour l'indice = 2, la sortie doit être,

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

Je vous écris le code qui fonctionne, j'essaie de trouver de façon plus concise (mais pas faible performance).

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];
}

Quelle est la façon simple pour l'accomplir?

Ayrton:

Vous pourriez créer une boucle un peu plus, en ajoutant les cordes à un second réseau:

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];
    }
}

Je suppose que tu aimes

Origine http://43.154.161.224:23101/article/api/json?id=222907&siteId=1
conseillé
Classement