views:

334

answers:

7
+2  Q: 

ArrayList in java

ArrayList<String> veri1 = new ArrayList<String>();

String[] veri2 = {"Fatih", "Ferhat", "Furkan"};

How can i add "veri2" to "veri1" like one element ? I mean if i call veri.get(0), it returns veri2..

Regards.

+2  A: 

I just saw (due to fm), that you have an ArrayList<String>. You can do:

ArrayList veri1 = new ArrayList();
veri1.add(veri2)

or

ArrayList<String[]> veri1 = new ArrayList<String[]>();
veri1.add(veri2)

You can also make ver1 a List, which gives you flexibility in changing implementations.

Matthew Flaschen
@Matthew Flaschen - that's not quite right, because the list is of type String, not type String[], so the compiler won't allow it.
justkt
@justkt, I didn't see that because `ArrayList<String>` was rendering as `ArrayList` until I fixed it.
Matthew Flaschen
Good stuff - thanks.
justkt
@Nils, I initially thought he had a `ArrayList`, because of misleading rendering.
Matthew Flaschen
and I were trying hard to find out why it was edited...
Carlos Heuberger
+12  A: 

You should declare your list as a list of string arrays, not a list of strings:

List<String[]> veri1 = new ArrayList<String[]>();
String[] veri2 = {"Fatih", "Ferhat", "Furkan"};

veri1.add(veri2);

Note that in general it is better to declare your list as List instead of ArrayList, as this leaves you the freedom to switch to a different list implementation later.

Péter Török
+1 as it seems to me the only correct solution. Nevertheless a sentence, that it is necessary to make it an List of String[] insteaod of List of String, would have been nice .
Nils Schmidt
@Nils Schmidt, good point, thanks - sentence added.
Péter Török
I think the point that needs to be stressed is that the OP needs to make a List of `String[]` (string arrays) as opposed to `String` (strings) for this to work with each item being a String array.
justkt
+4  A: 

You should use the List interface and generics (for Java >= 1.5). Depending on what you want to do you can use this:

String[] veri2 = {"Fatih", "Ferhat", "Furkan"};

List<String> veri1 = new ArrayList<String>();
veri1.addAll(Arrays.asList(veri2)); // Java 6

List<String[]> veri3 = new ArrayList<String[]>();
veri3.add(veri2);
Daff
+2  A: 

You can't actually do this.

veri2 is an array of strings, veri1 is an arraylist of individual strings Thus, doing veri1.get(0) should return a single string, not an array of strings.

Uri
+1  A: 

It all depends on whether or not you want your ArrayList to be of one type or if you need it to hold multiple types.

If you just need it to hold String arrays throughout your code, declare as stated above:

ArrayList<String[]> list1 = new ArrayList<String[]>();

then just add the String array to it as follows:

list1.add(stringArray);

If you want it to be dynamic, declare it with the object type:

ArrayList<Object> anythingGoes = new ArrayList<Object>();

and then you can add anything later on as well:

anythingGoes.add(stringArray);
anythingGoes.add(myAge);
anythingGoes.add(myName);
Scott
A: 

You pretty much just need to add the array to the ArrayList.

ArrayList<String[]> veri1 = new ArrayList<String[]>();
String[] veri2 = {"a", "b", "c"};
veri1.add(veri2);

System.out.println(veri1.size());
for(String[] sArray : veri1)
    for(String s : sArray)
        System.out.println(s);
Brent Parker
A: 

I think you mean this:

import java.util.Arrays;

ArrayList<String> veri1 = new ArrayList<String>();
String[] veri2 = {"Fatih", "Ferhat", "Furkan"};

veri1.addAll(Arrays.asList(veri2);

Cheers,

Jonas

Jonas Fagundes