Retrieving elemnts from an ArrayList by specifying the indexes - java

Is there a method in Java to get the list of objects from an Arraylist to another ArrayList, by just specifying the start and end index?

Yes you can use the subList method:
List<...> list2 = list1.subList(startIndex, endIndex);
This returns a view on that part of the original list, it does not copy the data.
If you want a copy:
List<...> list2 = new ArrayList<...> (list1.subList(startIndex, endIndex));

/create an ArrayList object
ArrayList arrayList = new ArrayList();
//Add elements to Arraylist
arrayList.add("1");
arrayList.add("2");
arrayList.add("3");
arrayList.add("4");
arrayList.add("5");
/*
To get a sub list of Java ArrayList use
List subList(int startIndex, int endIndex) method.
This method returns an object of type List containing elements from
startIndex to endIndex - 1.
*/
List lst = arrayList.subList(1,3);
//display elements of sub list.
System.out.println("Sub list contains : ");
for(int i=0; i< lst.size() ; i++)
System.out.println(lst.get(i));

Related

add data into an object list index by index

I have two lists of data with the same size.
I am adding the two lists to an array of objects but the Syllabus list is completely added to the object list before being added to the Article list. My challenge is add data to the Syllabus list and the Article list simultaneously.
For instance if the index of syllabus is zero, let it get the index of the Syllabus and of the Article and add it to the object list before moving
to the index of 1 till the last index so as the index of Syllabus grows so does the Article until they get to the last element in the list.
Below is my attempt but it's not working index by index
Here is the object list
#SuppressWarnings("unused")
List <Object> obj = new ArrayList<Object>();
List <Syllabus> syllabus = articleOfAssociationDao.findByType(type);
obj.addAll(syllabus); //adding to an object list
for(int i =0; i < syllabus.size(); i++) {
List<Article> articleContent = articleContentDao.findBySyllabus(syllabus.get(i));
obj.addAll( articleContent); //adding to an object list
}
Would the following work?
for(int i =0; i < syllabus.size(); i++) {
List<Article> articleContent = articleContentDao.findBySyllabus(syllabus.get(i));
obj.add(syllabus.get(i));
obj.addAll(articleContent);
}

How to initialize inner ArrayList of a two-dimensional ArrayList

The below ArrayList is a two-dimensional ArrayList of size parts. I'm divding the storeIds into parts of ArrayList and add them to the inner ArrayList of the 2D ArrayList.
ArrayList<ArrayList<String>> partStoreIds = new ArrayList<ArrayList<String>>(parts);
for(int i = 0; i < parts; i++)
{
System.out.println("Executing part: " + i);
int maxIndex = Math.min(storeIds.size(), querySize*(i+1));
//The below line is throwing an exception
partStoreIds.addAll(storeIds.subList(querySize*i, maxIndex));
}
What you try to achieve can be done as next:
partStoreIds.add(new ArrayList<>(storeIds.subList(querySize*i, maxIndex)));
Indeed, as partStoreIds is an ArrayList of ArrayList only ArrayList instances can be added and since storeIds.subList(querySize*i, maxIndex) returns a List, you need to convert it first as an ArrayList using the constructor new ArrayList(Collection).
But a much simpler approach would be to declare your partStoreIds as a List of List, then you can add your subList directly as next:
List<List<String>> partStoreIds = new ArrayList<>(parts);
...
partStoreIds.add(storeIds.subList(querySize*i, maxIndex));
You need to create a new ArraraList and then add items to it
ArrayList<String> temp=new ArrayList<String>();
temp.addAll(storeIds.subList(querySize*i, maxIndex));
partStoreIds.add(temp);

Type mismatch when creating List of objects

I am trying to part the mainListarraylist to 3 sublist Arraylist and then add it as sublist in other arraylist mainSublist but I am getting this error:
How can I fix it?
I appreciate any help.
1)-Type mismatch: cannot convert from List to ArrayList
2) -The method add(RootCreator) in the type ArrayList is not applicable for the arguments (ArrayList)
Code:
ArrayList<RootCreator> mainList = new ArrayList<RootCreator>();
for (String key : names) {
RootCreator rootcreat = join_line(path, key);
mainList.add(rootcreat);
}
ArrayList<RootCreator> mainSublist = new ArrayList<RootCreator>();
for(int i= 0 ; i < mainList.size(); i++){
int index = i*3;
//the error 1 is here
ArrayList<RootCreator> sublist = mainList.subList(0, index);
//error 2 is here
mainSublist.add(sublist);
}
All List are not ArrayList, so when you use sublist you get a generic List. On the other hand, the method to add a collection to another collection is addAll instead add.
List<RootCreator> mainList = new ArrayList<RootCreator>();
for (String key : names) {
RootCreator rootcreat = join_line(path, key);
mainList.add(rootcreat);
}
List<RootCreator> mainSublist = new ArrayList<RootCreator>();
for(int i= 0 ; i < mainList.size(); i++){
int index = i*3;
//the error 1 is here
List<RootCreator> sublist = mainList.subList(0, index);
//error 2 is here
mainSublist.addAll(sublist);
}
The subList method returns a List, not an ArrayList, and you cannot assign a List to an ArrayList. In fact, the List returned by the subList method is not an ArrayList.
If it needs to be an ArrayList, then create an ArrayList out of the sub list.
ArrayList<RootCreator> sublist = new ArrayList<>(mainList.subList(0, index));
If you just need a List, then make sublist a List instead.
List<RootCreator> sublist = mainList.subList(0, index);
You can't add an ArrayList<RootCreator> to an ArrayList<RootCreator>; you must add a RootCreator. If you want to add the elements of sublist to another list, then use the addAll method.
mainSublist.addAll(sublist);
OK, let's look at the two errors here:
//the error 1 is here
ArrayList<RootCreator> sublist = mainList.subList(0, index);
This is telling you that you're trying to assign something of the type List to a variable that's declared as an ArrayList. While an ArrayList is a subtype of List, it doesn't work both ways - you can assign an ArrayList to a List, but not vice versa. So let's change your declaration:
//the error 1 was here, but fixed.
List<RootCreator> sublist = mainList.subList(0, index);
The second one is slightly different:
//error 2 is here
mainSublist.add(sublist);
Here, you've got a list defined to hold items of the type RootCreator - but you're not adding RootCreator items, you're adding a list of them. So if you want to have the list hold other lists, you need to specify that when you create it:
ArrayList<List<RootCreator>> mainSublist = new ArrayList<List<RootCreator>>();
But, if you're trying to just keep a single list and want to add everything from the sublists, then instead, change your code to:
//error 2 was here, but Fixed
mainSublist.addAll(sublist);

Accessing Arraylist of Arraylist<ArrayList>

I have an arraylist of an arraylist declared like
ArrayList<ArrayList<Integer>> bigList = new ArrayList<ArrayList<Integer>>();
I would then add to this bigList by bigList.add(arraylist).
I then have a class that takes ArrayList<Integer> as a construcor parameter. My question is how do I send a certain ArrayList in bigList to this class as a constructor parameter? I can iterate through my bigList with
for(ArrayList<Integer> list : bigList) {
for(Integer num : list)
System.out.println(num);
}
but I have not been able to send a whole ArrayList element to another class. Thanks a bunch.
You can select an ArrayList at a particular index and pass it to a constructor as follows:
int index = 2; // the index of the list you want to pass to the constructor
MyNewObject newObject = new MyNewObject(bigList.get(index));
Have you tried:
List<SomeClass> sList = new ArrayList<>();
for(ArrayList<Integer> list : bigList) {
sList.add(new SomeClass(list));
}
This would work fine if you are looking to iterate though all lists in bigList and construct new instances from all of them. Alternatively, if you just want to create one such object for a specific list at index "i" of bigList:
SomeClass s = new SomeClass(bigList.get(i));

How to use ArrayList's get() method

I'm new to java (& to OOP too) and I'm trying to understand about the class ArrayList
but I don't understand how to use the get(). I tried searching in net, but couldn't find anything helpful.
Here is the official documentation of ArrayList.get().
Anyway it is very simple, for example
ArrayList list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
String str = (String) list.get(0); // here you get "1" in str
To put it nice and simply, get(int index) returns the element at the specified index.
So say we had an ArrayList of Strings:
List<String> names = new ArrayList<String>();
names.add("Arthur Dent");
names.add("Marvin");
names.add("Trillian");
names.add("Ford Prefect");
Which can be visualised as:
Where 0, 1, 2, and 3 denote the indexes of the ArrayList.
Say we wanted to retrieve one of the names we would do the following:
String name = names.get(1);
Which returns the name at the index of 1.
So if we were to print out the name System.out.println(name); the output would be Marvin - Although he might not be too happy with us disturbing him.
You use List#get(int index) to get an object with the index index in the list. You use it like that:
List<ExampleClass> list = new ArrayList<ExampleClass>();
list.add(new ExampleClass());
list.add(new ExampleClass());
list.add(new ExampleClass());
ExampleClass exampleObj = list.get(2); // will get the 3rd element in the list (index 2);
ArrayList get(int index) method is used for fetching an element from the list. We need to specify the index while calling get method and it returns the value present at the specified index.
public Element get(int index)
Example :
In below example we are getting few elements of an arraylist by using get method.
package beginnersbook.com;
import java.util.ArrayList;
public class GetMethodExample {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("pen");
al.add("pencil");
al.add("ink");
al.add("notebook");
al.add("book");
al.add("books");
al.add("paper");
al.add("white board");
System.out.println("First element of the ArrayList: "+al.get(0));
System.out.println("Third element of the ArrayList: "+al.get(2));
System.out.println("Sixth element of the ArrayList: "+al.get(5));
System.out.println("Fourth element of the ArrayList: "+al.get(3));
}
}
Output:
First element of the ArrayList: pen
Third element of the ArrayList: ink
Sixth element of the ArrayList: books
Fourth element of the ArrayList: notebook
Would this help?
final List<String> l = new ArrayList<String>();
for (int i = 0; i < 10; i++) l.add("Number " + i);
for (int i = 0; i < 10; i++) System.out.println(l.get(i));
The get() method returns an element. For example:
ArrayList<String> name = new ArrayList<String>();
name.add("katy");
name.add("chloe");
System.out.println("The first name in the list is " + name.get(0));
System.out.println("The second name in the list is " + name.get(1));
The output:
The first name in the list is katy
The second name in the list is chloe

Categories

Resources