i have arraylists named sub and main,
ArrayList main = new ArrayList();
ArrayList sub=new ArrayList();
i add value to sub and then add sub to main.
example;
sub.add(1);
sub.add(2);
main.add(sub);
now i want to get all values inside sub
so i used following one but .get(j) gives me the error get >> canot find symbol
for (int i=0;i<main.size();i++) {
System.out.println();
for (int j=0;j<sub().size();j++) {
System.out.print(main.get(i).get(j));//error line
}
}
how can i get all values inside subarray of main arraylist
When you declare a variable as
ArrayList main;
This list holds Objects. This means that main.get(i) will only return an Object, even if you add ArrayLists. That's why you get a compiler error: Object doesn't have a method named get().
To fix the problem, you need to use generics:
ArrayList<List<Integer>> main = new ArrayList<>();
ArrayList<Integer> sub=new ArrayList<>();
Now get() will return a List<Integer> which has a get() method, so the compiler error will disappear.
Generics could be your friend here:
ArrayList<ArrayList<Object>> main = new ArrayList<ArrayList<Object>>(); // or new ArrayList<>(); in Java 7+
ArrayList<Object> sub = new ArrayList<Object>(); // or new ArrayList<>();
If you can't or don't want to use generics, the solution is to cast the expression main.get(i) to an ArrayList first:
System.out.println(((ArrayList) main.get(i)).get(j));
Go through the following code
public class ArrayListDemo {
public static void main(String[] args) {
List<List<Integer>> main = new ArrayList<>();
List<Integer> sub = new ArrayList<>();
sub.add(1);
sub.add(2);
main.add(sub);
//If you want to get values in sub array list
for(int i = 0; i < 1; i++){
List<Integer> arr = main.get(i);
for(Integer val : arr) System.out.println(val + "");
}
//If you want to print all values
for(List<Integer> list : main){
for(Integer val : list) System.out.println(val + "");
}
}
}
In the above code, I had declared an ArrayList (main) to keep all Array which are having Integer values. Also i had declared an another ArrayList (sub) to keep all Integer values.
I had used ArrayList data structure because of length of the List will be changing the
run time.
Good Luck !!!
Related
I am having issue to store all values of ArrayList<ArrayList<String>> into ArrayList<String>. Here stylistIDArray and durationArray are array of array. I want to store all their values in stylistId and duration respectively. The stylistid and duration are array of string.
Here's my attempt, but it stores only the last item of each array of array.
ArrayList<ArrayList<String>> stylistIDArray;
ArrayList<ArrayList<String>> durationArray;
stylistIDArray = differentGenderServicesAdapter.getSelectedStylistIdArray();
durationArray = differentGenderServicesAdapter.getSelectedDurArray();
ArrayList<String>stylistId = new ArrayList<>();
ArrayList<String>duration = new ArrayList<>();
for(int i=0; i<stylistIDArray.size(); i++) {
stylistId = stylistIDArray.get(i);
duration = durationArray.get(i);
}
Note : I have already tried this, but doesn't work for me.
To be generic, First let be an list of list of objects
List<List<Object>> listOfList;
That you want to put into a list of object
List<Object> result;
Note the result list will contain every object contain is the input list. This transformation will loose the information of which object was in which list.
You have to loop through the listOfList. At each loop you obtain a list of object (List<Object> listOfObject). Then loop through these lists to obtain every object (Object o). Then add these object to the result list (result.add(o)).
for(List<Object> listOfObject : listOfList) {
for(Object o : listOfObject) {
result.add(o);
}
}
In your case, the problem is that you use affectation instead of add(). At every loop this replaces the value by the new one. So at the end you have stored only the last item of each list.
stylistId=stylistIDArray.get(i); //This replace the existing stylistId
Instead try something like
ArrayList<ArrayList<String>> stylistIDArray;
ArrayList<ArrayList<String>> durationArray;
stylistIDArray = differentGenderServicesAdapter.getSelectedStylistIdArray();
durationArray = differentGenderServicesAdapter.getSelectedDurArray();
ArrayList<String> stylistId = new ArrayList<>();
ArrayList<String> duration = new ArrayList<>();
for(ArrayList<String> l : stylistIDArray) {
for(String s : l) {
stylistId.add(s);
}
}
for(ArrayList<String> l : durationArray ) {
for(String s : l) {
duration.add(s);
}
}
You doing wrong operation with arraylist, in loop you are getting data from stylistIDArray and assign to aryalist, not inserting in list, have look this
stylistIDArray=differentGenderServicesAdapter.getSelectedStylistIdArray();
durationArray=differentGenderServicesAdapter.getSelectedDurArray();
ArrayList<String>stylistId=new ArrayList<>();
ArrayList<String>duration=new ArrayList<>();
for(int i=0; i<stylistIDArray.size(); i++) {
stylistId.add(stylistIDArray.get(i));
duration.add(durationArray.get(i));
}
Hope it will help you!
public void verProduto(){
//System.out.println("Digite o codigo do produto : - ");
//produt.setCodigo(scan.nextInt());
List<String> list = (List<String>) jeproduto.keys("*");
for(int i = 0; i<list.size(); i++) {
System.out.println("List of stored keys:: "+list.get(i));
}
}
This code returns error:
java.util.HashSet cannot be cast to java.util.List
Could someone help?
You havent given a lot of detail but you can try:
List<String> list = new ArrayList<String>(jeproduto.keys("*"));
Assuming you are using jedis and that the jeproduto object is a Jedis instance, per the javadocs, the keys method returns the type Set<String>
You have two options:
Change your collection type to a Set instead of a List:
Set<String> keySet = jeproduto.keys("*");
for (String key : keySet) {
System.out.println("List of stored keys: " + key);
}
Or
If you truly need a List, do what CannedMoose mentioned and take advantage of the ArrayList constructor that allows you to pass another collection.
List<String> list = new ArrayList<>(jeproduto.keys("*"));
Since your code is not fully completed.
My Assumption is
jeproduto = new HashSet<String>
then
List<String> list = (List<String>) jeproduto.keys("*"); line should be
// Creating a List of HashSet elements
List<String> list = new ArrayList<String>(hset);
if you want to convert Hashset to List, please find the sample code below.
public static void main(String[] args) {
// Create a HashSet
HashSet<String> hset = new HashSet<String>();
//add elements to HashSet
hset.add("A");
hset.add("B");
hset.add("C");
hset.add("D");
hset.add("E");
// Displaying HashSet elements
System.out.println("HashSet contains: "+ hset);
// Creating a List of HashSet elements
List<String> list = new ArrayList<String>(hset);
// Displaying ArrayList elements
System.out.println("ArrayList contains: "+ list);
}
I have one arraylist that contain two list
like this
[[asd, asswwde, efef rgg], [asd2223, asswwd2323e, efef343 rgg]]
My Code is
ArrayList<String> create = new ArrayList<String>();
ArrayList<String> inner = new ArrayList<String>();
ArrayList<String> inner1 = new ArrayList<String>();
inner.add("asd");
inner.add("asswwde");
inner.add("efef rgg");
inner1.add("asd2223");
inner1.add("asswwd2323e");
inner1.add("efef343 rgg");
create.add(inner.toString());
create.add(inner1.toString());
i have to get all value one by one of every index of that arraylist
So what is the best way to get these all value one by one.
I am using JAVA with Eclipse Mars.
Just use two nested loops:
List<List<Object>> list = ...;
for (List<Object> subList : list) {
for (Object o : subList) {
//work with o here
}
}
You may also want to consider replacing the inner lists by proper objects.
You want to loop through the outside ArrayList and then loop through each ArrayList within this ArrayList, you can do this by using the following:
for (int i = 0; i < outerArrayList.size(); i++)
{
for (int j = 0; j < outerArrayList.get(i).size(); j++)
{
String element = outerArrayList.get(i).get(j);
}
}
Here is another verison you may find easier to understand, but is essentially the same:
for (int i = 0; i < outerArrayList.size(); i++)
{
ArrayList<String>() innerArrayList = outerArrayList.get(i)
for (int j = 0; j < innerArrayList.size(); j++)
{
String element = innerArrayList.get(j);
}
}
or alternatively again using a foreach loop:
for (ArrayList<String> innerArrayList : outerArrayList)
{
for (String element : innerArrayList)
{
String theElement = element;
}
}
It might be worth noting that your ArrayList appears to contain different types of elements - is this definitely what you wanted to do? Also, make sure you surround your strings with "" unless they are variable names - which it doesn't appear so.
EDIT: Updated elements to type String as per your update.
I would also recommend you change the type of your create ArrayList, like below, as you know it will be storing multiple elements of type ArrayList:
ArrayList<ArrayList> create = new ArrayList<ArrayList>();
Try to use for loop nested in foreach loop like this:
for(List list : arrayListOfList)
{
for(int i= 0; i < list.size();i++){
System.out.println(list.get(i));
}
}
I'm not sure if the data structures are part of the requirements, but it would be better constructed if your outer ArrayList used ArrayList as the generic type.
ArrayList<ArrayList<String>> create = new ArrayList<ArrayList<String>>();
ArrayList<String> inner = new ArrayList<String>();
ArrayList<String> inner1 = new ArrayList<String>();
...
create.add(inner);
create.add(inner1);
Then you could print them out like this:
for(List list : create) {
for (String val : list) {
System.out.println(val);
}
}
Othewise, if you stick with your original code, when you add to the outer list you are using the toString() method on an ArrayList. This will produce a comma delimited string of values surrounded by brackets (ex. [val1, val2]). If you want to actually print out the individual values without the brackets, etc, you will have to convert the string back to an array (or list) doing something like this:
for (String valList : create) {
String[] vals = valList.substring(1, val.length() - 1).split(",");
for (String val : vals) {
System.out.println(val.trim());
}
}
i am trying to implementing ArrayList using String array.
while implementing i am getting Remove type arguments error in my eclipse.
ArrayList.java
import java.util.List;
public class ArrayList {
public static void main(String [] a)
{
String [] things = {"eggs","chicken","milk","butter"};
List<String> list1 = new ArrayList<String>();
for(String s: things)
list1.add(s);
String [] morethings = {"chicken","milk"};
List<String> list2 = (List<String>) new ArrayList ();
for(String y: morethings)
list2.add(y);
for(int i=0; i<list1.size();i++)
{
System.out.printf("%s ", list1.get(i));
}
}
}
You've defined your own ArrayList which is not a generic class. To use java.util.ArrayList simply rename your class to something else other than one of Java's built-in classes
You have used the class java.awt.List which is used for GUI List options such as creating a drop-down list, and thus one cannot use collectibles/data structure, Iterators with such a list.
You need to use java.util.List for implementing:
List>= new LinkedList>();
I'm facing a problem in ArrrayList. Please help.
A test program in which has two data type String and int,
when add item by add method it give me error;
if Integer is add The method add(int, Test) in the type ArrayList is not applicable for the arguments (int)
and if String is add it give
The method add(Test) in the type ArrayList is not applicable for the arguments (String)
public cl* Test {
String firstName;
int rollNum;
public static void main(String[] args) {//this ArrayList work fine
ArrayList<Integer> Num=new ArrayList<Integer>();
for (int i = 0; i <10; i++) {
Num.add(i);
}
for (int i = 0; i < Num.size(); i++) {
System.out.print(Num.get(i));
}//this ArrayList work fine
ArrayList<String> Num2=new ArrayList<String>();
Num2.add("ali");
Num2.add("2");
ArrayList<Test> Num1=new ArrayList<Test>();
Num1.add("Char"); // now it generate Error
}
ArrayList<Integer> a means -> Creating an ArrayList of Integer objects.
ArrayList<String> b means -> Creating an ArrayList of String objects.
Similarly,
ArrayList<Test> c means -> Creating an ArrayList of Test objects.
So, you need to create a Test object to add it to the c list as it demands a Test object.
Now,
/* If you have this parameterized constructor.
I suggest you to have it if you dont have. */
Test obj = new Test("Fname",10);
obj can be inserted now in the c object. This could be implemented using the add method of the
ArrayList class.
c.add(obj); //Adds the object 'obj' to the ArrayList 'c'
This won't give error.
In the last statement you have created an array list of type Test
ArrayList<Test> Num1=new ArrayList<Test>();
So insert Test objects into it
For eg:-
Test test = new Test();
test.setFirstName("ABC");
test.setRollNo(23);
ArrayList<Test> Num1 = new ArrayList<Test>();
Num1.add(test);