-5

I have a String as shown below

String result = ["A","B","C","D","E","F"]
List list = new ArrayList();

        list.add("One");
        list.add("One2");
        list.add("One3");

I have an ArrayList , and i want to add the String result , so that it should look like

["A","B","C","D","E","F","One","One2","One3"]

I have tried this way

String result = ["A","B","C","D","E","F"]
        for(int i=0;i<list.size();i++)
        {
        result = result+"list.get(i)"
        }

could anybody please help .

Pawan
  • 31,545
  • 102
  • 256
  • 434
  • 1
    `["A","B","C","D","E","F"]` is not a String. Can you clarify your intent? – assylias Jan 31 '13 at 14:08
  • See i get a response from a server that way , so for standalone purpose i did so – Pawan Jan 31 '13 at 14:08
  • 1
    If it is a JSON response then you need to parse it using an appropriate object mapper (you may use the Jackson library for that purpose as I usually do). – Lion Jan 31 '13 at 14:10
  • 1
    @PreethiJain: If you're going to write something which looks like code, please make it *real code*. Otherwise it's very hard to know what you're really trying to achieve. – Jon Skeet Jan 31 '13 at 14:11
  • it looks like java code, but definitely it's not – pbaris Jan 31 '13 at 14:13
  • It's either `String result = "ABCDEF"` (a string) or `String[] result = {"A","B","C","D","E","F"}` (an array of strings), pick one – Bernhard Barker Jan 31 '13 at 14:14
  • 1
    Please have a look on my post to see another approach to solv your issue – Festus Tamakloe Jan 31 '13 at 14:32

7 Answers7

2

This is a pretty way to do that without too many walk around

import java.util.ArrayList;
import java.util.List;

public class Tester4 {

    public static void main(String args[]) {

        String[] result = new String[] { "A", "B", "C", "D", "E", "F" };

        List<String> list = new ArrayList<String>();

        list.add("One");
        list.add("One2");
        list.add("One3");

        for(int i = result.length-1; i>= 0; i--) {

            list.add(0,result[i]);
        }

        for(String string : list) {
            System.out.println("Answer: " + string);
        }

    }

}
Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
1

as @assylias pointed out, your question seems to be unclear. Assuming that your variable "result" is a string, do the following:

String result = "\"A\",\"B\",\"C\",\"D\",\"E\",\"F\"";
List list = new ArrayList();

list.add("One");
list.add("One2");
list.add("One3");

for(int i=0;i<list.size();i++)
{
   result += ",\"" + list.get(i) +"\""
}

Then your output from "result" should be:

"A","B","C","D","E","F","One","One2","One3"
alex
  • 5,516
  • 2
  • 36
  • 60
0

["A","B","C","D","E","F"] is not a String. try this...

String result = ""
for(int i = 0; i < list.size(); i++){
    result += list.get(i);
}

Greetings.

MG_Bautista
  • 2,593
  • 2
  • 18
  • 33
0

The variable "result" that you use is an Array of String, not a String.

If you want to have a list of string with those charatestics you should write:

String[] result = {"A","B","C","D","E","F"};
ArrayList<String> list = new ArrayList<String>();
list.add("One");
list.add("One2");
list.add("One3");
for(int i=0;i<result.length;i++)
{
   list.add(i, result[i]);
}

So you will put the elements of the array "result" in the list before the others.

Ikki
  • 227
  • 3
  • 12
  • This is inefficient, you'll be moving those 3 "One" elements continuously. Rather start with something like `list = new ArrayList(Arrays.asList(result))` and append the `One`, `One2` and `One3`. Not to mention it's not compilable code. – Bernhard Barker Jan 31 '13 at 14:18
0

Result isn´t a String, it´s a String[]. So, if you want result to look like

["A","B","C","D","E","F","One","One2","One3"]

you need to put "One", "Two", "Three" in another String[] and then concatenate the two arrays, for example like it´s shown here :

Community
  • 1
  • 1
Jannis Alexakis
  • 1,279
  • 4
  • 19
  • 38
0
list.addAll(Arrays.asList(result)).toArray(new String[0]);
bwt
  • 17,292
  • 1
  • 42
  • 60
0

In Java defining the array looks like: String[] result = {"A","B","C"}

String[] result = {"A","B","C","D","E","F"};
List list = new ArrayList();

list.add("One");
list.add("One2");
list.add("One3");

List<String> newList = new ArrayList(Arrays.asList(result));
newList.addAll(list);

Output newList:

[A, B, C, D, E, F, One, One2, One3]

isvforall
  • 8,768
  • 6
  • 35
  • 50