ArrayList in java - 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.

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.

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);

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.

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.

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);

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);

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);

Related

How to combine List String array to single array android

I want to combine two List Array into a Array single one.
first List looks like this:
List<String> first = new ArrayList<String>();
{a,s,d,f,g,h}
second list looks like this:
List<String> second = new ArrayList<String>();
{z,x,c,v,b}
I did the following:
ArrayList<String> combine = new ArrayList<String>();
combine.addall(first);
combine.addall(second);
{a,s,d,f,g,h,z,x,c,v,b}
But I want to combine both to be as
{{a,s,d,f,g,h},{z,x,c,v,b}}
How to do this in Java?
You should do something like this
ArrayList<String> l1=new ArrayList<String>();
l1.add("a");
ArrayList<String> l2=new ArrayList<String>();
l1.add("z");
List<ArrayList<String>> l3 = new ArrayList<ArrayList<String>>();
l3.add(l1);
l3.add(l2);
l1 and l2 are lists of strings, while l3 is a list of listOfStrings.
But are you sure you really want to do that? What about multidimensional arrays?

Add String Arrays in ArrayList

I have an ArrayList defined as:
ArrayList<String[]> params=new ArrayList<String[]>();
It contains parameters ("name", value) in String Arrays. I would like to insert elements in the ArrayList:
params.add({"param1", param1});
But when I try that I get an error.
What is the simplest way to add String Arrays in ArrayList. Do I have to declare a new array each time?
A declaration is the only time you can just use braces, e.g.
String[] test = {"param1", param1};
In all other times, you must use new String[] also.
params.add(new String[] {"param1", param1});
Make a string with some special sequence. Add it in ArrayList and then split it when you need it. For example:
ArrayList<String> str_list = new ArrayList<String>();
String str = "name&&&value";
// Add str to str_list
str_list.add(str);
Then fetch it from arraylist and split it using following code:
String str1 = str_list.get(index);
String[] values = str1.split("&&&");
values[0] will be name and values[1] will be value.
You should read up on ArrayLists here
But after initialization you can do this:
params.add("String");
params.add(aStringObject);
Your initialization is incorrect as well; If you want a ArrayList of Strings it should be:
ArrayList<String> params = new ArrayList<String>();

java Collection FrameWork

i want to store Arraylist inside Another List, some thing like this i know its wrong but pretty much like this....
List list=new ArrayList();
list.add(new("element1","element2","element3",element4=?);
list.add(new("element5","element6","element7",element8=?);
now if u see the above code its ok and fine to add elements or first row to list up to third element but the fourth element is another array[string array] how to add it or append it to the first row of element.
same with the second row.
NOTE:- element4 and element8 are also differ in length means element4 has 2 string and element 8 has 10 strings.
when i display the list it should show list like this
The first row of list is
element1 element2 element3 element4.1 element4.2
the second row of list is
element5 element6 element7 element8.1 element8.2 element8.3 element8.4 element8.5 element8.6 element8.7 element8.9 element8.10
So from what I understood about your question, you need a List<List<String>>. Do it as follows:
List<String> strs1 = new ArrayList<String>();
strs1.add("element1");
strs1.add("element2");
List<String> strs2 = new ArrayList<String>();
strs2.add("element3");
And then
List<List<String>> listOfList = new ArrayList<List<String>>();
listOfList.add(strs1);
listOfList.add(strs2);
OR
List<List<String>> asList = Arrays.asList(strs1, strs2);
Have you tried following way:
List list = new ArrayList();
list.add(Arrays.asList("element1", "element2", "element3",
Arrays.asList("element4.1", "element4.2")));
list.add(Arrays.asList("element5", "element6", "element7",
Arrays.asList("element8.1", "element8.2", "element8.3")));
// print values
System.out.println(list.get(0));
System.out.println(list.get(1));
Unify it to a List of lists of lists, a three dimensional matrix - even if your first elements are single values, it makes sense to wrap them in lists just to simplify the code:
List<List<List<String>>> matrix = new ArrayList<List<List<String>>>();
List<List<String>> row = new ArrayList<List<String>>();
matrix.add(row);
List<String> column1 = new ArrayList<String>();
column1.add("element1");
row.add(column1);
List<String> column2 = new ArrayList<String>();
column1.add("element2.1");
column1.add("element2.2");
row.add(column2);
You can add lists to other lists by doing something like so: List<List<String>> myList = new ArrayList<List<String>>();.... However, seeing that you are adding items which have a different type, I would recommend you do the following (assuming you always have 3 array lists and 1 array):
Create a new class which takes in 4 arguments, these being the 3 array lists and the 1 array.
Have your class override its own toString() method in such a way that it will iterate over the elements and print their content in whatever way you would like.
Create an list using generics, using something like this: List<MyClass> myList = new ArrayList<MyClass>();.... In this case, MyClass is the class I have described in point 1. This will allow you to create a type safe structure which does not need to do any casting, thus making your code look cleaner and probably run slightly faster.
Seeing that you say that the elements can contain list of strings of various lengths, you can do something like this:
public class MyClass
{
private List<String> arrayList1;...
private String[] myArray;...
public MyClass(List<String> list1, ..., String[] myArray)
{
this.arrayList1 = list1;
this.myArray = myArray;
...
}
...
#Override
public String toString()
{
StringBuilder sb = new StringBuilder();
//iterate adding your list contents to your string builder.
return sb.toString();
}
}
Adding your elements then will be something like this:
...
List<String> arrayList1 = ...;
arrayList1.add("...");...
String[] myArray = ...;
MyClass myClass = new MyClass(arrayList1, ..., myArray);
System.out.println(myClass.toString());

Adding item to multidimensional ArrayList

Hello only have a few hours with Java. (from Python)
I am trying to define a multidimensional array and populate it with the "add" method. however, I am seeing some bizarre results:
List<String[]> DrawInstructions = new ArrayList<String[]>();
String[] pair = {"",""};
pair[0]="UW";
pair[1]="100";
DrawInstructions.add(pair);
pair[0]="UM";
pair[1]="10";
DrawInstructions.add(pair);
pair[0]="UT";
pair[1]="50";
DrawInstructions.add(pair)
I expected DrawInstructions to end up with this:
[("UW,"100"),("UM","10"),("UT","50")]
But instead I am getting:
[("UT","50"),("UT","50"),("UT","50")]
I am sure this is pretty elemental but I cant figure it out, I have searched for a couple of hours. Thank you for any advice.
Problem is, you're changing the same array over and over — .add() does not create a copy for you. Try the following:
List<String[]> DrawInstructions = null;
String[] pair = {"",""};
pair[0]="UW";
pair[1]="100";
DrawInstructions.add(pair);
pair = new String[] {"",""};
pair[0]="UM";
pair[1]="10";
DrawInstructions.add(pair);
pair = new String[] {"",""};
pair[0]="UT";
pair[1]="50";
DrawInstructions.add(pair);
or, better then,
List<String[]> drawInstructions = new ArrayList<String[]>();
drawInstructions.add(new String[] {"UW", "100"});
drawInstructions.add(new String[] {"UM", "10"});
drawInstructions.add(new String[] {"UT", "50"});
which would be closer to Java naming standards, avoid NPEs, minimize local state, and be arguably more readable.
You are adding the same array (pair) three times. All you do is changing it's value(content).
Remember that java operates on references, not on the copies of the objects.
Try to create pair1, pair2, pair3.
You defined only one String[] pair = {"",""};
You edited the same object 3 times and added that to the list.
You could do that:
List<String[]> DrawInstructions = new ArrayList<String[]>();
String[] pair1 = {"",""};
pair1[0]="UW";
pair1[1]="100";
DrawInstructions.add(pair1);
String[] pair2 = {"",""};
pair2[0]="UW";
pair2[1]="100";
DrawInstructions.add(pair2);
....

Java ArrayList type issue

I am new in Java.
Now I want to generate an ArrayList containing some values.
"Circle","blue","red","yellow","1","2","3","4"
How can I code this. I found some tutorial from internet. Only int or string accepted? How about mix? Could someone should me the code that how to do this?
Thanks!
List<String> list = Arrays.asList("Circle", "blue", "red", "yellow", "1", "2", "3", "4");
If you want to mix types, you'd need a List<Object>, and to remove the "" around the numbers. The example you show is all strings.
Once you start mixing types, you need to check the type when you're consuming the list, which may or may not be appropriate.
ArrayList<String> al = new ArrayList();
al.add("Circle");
al.add("blue");
al.add("red");
al.add("yellow");
al.add("1");
al.add("2");
al.add("3");
al.add("4");
here is a simple tutorial http://www.java-samples.com/showtutorial.php?tutorialid=234
or you can do this as well
String[] words = {"Circle", "blue", "red", "yellow", "1", "2", "3", "4"};
List<String> wordList = Arrays.asList(words);
List<Object> list = new ArrayList<Object>();
t.add("string");
t.add(5);
Or
List<Object> list = Arrays.asList("string", 5);
Or
List<Object> list = new ArrayList<Object>()
{{
add("string");
add(5);
}};
You only have one type for one list, some code for creating a list containing only Strings could be:
ArrayList<String> list = new ArrayList<String>();
//add my text as the first element
list.add("my text");
For a list with only ints you would have Integer instead of String in the example.
If you want to store "1","2","3","4" as string you could use
ArrayList<String> list = new ArrayList<String>();
Collections.addAll("Circle","blue","red","yellow","1","2","3","4");
You can not store int in any collection.However If you want to store "1","2","3","4" as Integer along with strings you could use
ArrayList<Object> list = new ArrayList<Object>();
Collections.addAll("Circle","blue","red","yellow",1,2,3,4);
Autoboxing will takecare of converting int to Integer
You many need to be extra careful while using ArrayList<Object>.
In Java, it's not recommended (although it's possible) to mix different types in a list of objects. So, for storing a list of Strings you would do this:
ArrayList<String> stringList = new ArrayList<String>();
And then add them:
stringList.add("Circle");
stringList.add("blue");
stringList.add("red");
stringList.add("yellow");
stringList.add("1");
stringList.add("2");
stringList.add("3");
stringList.add("4");

Categories

Resources