I have an ArrayList of ArrayLists of Integers. [ArrayList > list]. How do I set value foe an index of any inner ArrayList? I do not want to build an ArrayList of Integers and add them to the List of List using add function, but I want to set values to it, as I already have the List of List.
I'm assuming you have something like:
List<List<Integer>> list = new ArrayList<>(outerSize);
You've also said:
I do not want to build an ArrayList of Integers and add them to the List of List using add function, but I want to set values to it, as I already have the List of List.
If that's true, it means you've already done this or similar:
for (int i = 0; i < outerSize; ++i) {
List<Integer> inner = new ArrayList<>(innerSize);
for (int j = 0; i < innerSize; ++i) {
inner.add(defaultValue);
}
list.add(inner);
}
So given all of that, it's just get (to get the inner list from the outer list) and set (to set a value on it):
list.get(outerIndex).set(innerIndex, value);
Related
I had a requirement to loop through a list using indices in drools file because I wanted to compare an value in the list to its next value in the list. One way was to use from keyword to get individual list items but I needed indices too
value : String() from $listOfString
solved it by accessing the list in when, and for-looping through it in then clause
rule "Iterate and compare"
when
$list : ArrayList()
then
for (int i = 0; i < $list.size(); i++) {
if (list.get(i) comapred to list.get(i+1)) {
// insert/aggregate the result
}
}
end
I'm a beginner in Java and I'm trying to create an ArrayList that stores ArrayLists which hold integers. I already know the size I want for the outer ArrayList and want to store the inner ArrayLists. I would then for example want to add all dog ID numbers to the ArrayList stored in the first index of the outer ArrayList, cat ID numbers in the second, and lizard ID numbers in the third.This will happen in a random order so I want the outer ArrayList to have it's size already initialized so I can send an ID number to it's correct inner list when I come across it . This is what I did:
ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>(capacity);
ArrayList<Integer> inner = new ArrayList<Integer>();
for (int i = 0; i < capacity; i++) {
outer.add(null);
}
Would initializing those indices to null increase the size to whatever number is stored in capacity and allow me to later add a dog to inner by doing outer.get(3).add(10) and then outer.get(0).add(22)?
I also thought about doing this:
ArrayList<ArrayList<Integer>> outer = new ArrayList<ArrayList<Integer>>(capacity);
ArrayList<Integer> inner = new ArrayList<Integer>();
for (int i = 0; i < capacity; i++) {
outer.add(inner);
}
But don't know if adding inner would actually initialize all indices in outer to Integer ArrayLists or if they would be referencing the "inner" variable itself or something like that.
ArrayList has size (the number of elements) and capacity (the number of elements it can hold before it needs to automatically expand) which can be used for optimization, for now you can safely ignore capacity and just add and remove elements based on your needs.
Create the outer array with:
ArrayList<ArrayList<Integer>> outer = new ArrayList<>();
And then add all the inner arrays you need:
for (int i = 0; i < capacity; i++) {
outer.add(new ArrayList<Integer>());
}
I'm new to Java. I need a way to insert value to Map in List<List<Map<Integer,Integer>>> at a specyfic position. Any ideas how to do it?
E.g. i need to work on my list like on 2d array, so for instance I need to insert value at tab[0][0] = value. I need similar operation on List<List<Map<Integer,Integer>>>
Access by index on lists is done via List.get(int). So, write:
tab.get(0).set(0, newMap);
Of course, your lists must be correctly initialised to the appropriate sizes for get(int) and set(int) to work
You are trying to convert nested list into array or you want to map values to array. Here is a small snippet how to achieve it.
List<List<Map<Integer, Integer>> list = new ArrayList<List<Map<Integer, Integer>>();
for(int i = 0; i<array.length; i++){
for(int j=0;j<array[i].length;j++){
array[i][j] = list.get(i).get(j);
/*
list.get(i) retrieves the i row
.get(j) retrieves the column
*/
}
}
I'm working with Processing and IGeo library and I have an ArrayList of IVec[] arrays:
ArrayList<IVec []> v = new ArrayList<IVec[]>();
For every I of the ArrayList I have a collection of IVec [] arrays that represent the coordinates of the control points of a curve. I need to reverse the order of the IVec [] control points keeping the same order of the ArrayList (I'm trying to invert curve seam reversing control points order and keeping the original order of the curves) but I can't understand how to do this.
Can anyone help me?
I won't provide you a full solution, but will guide you through it:
Iterate on the array list v
for each item in it (IVec[]),
Convert the array to a collection (for example using Arrays.asList)
Use Collections.reverse to reverse the items in the list
Conver it back to an array
You can use Collections.reverse
You can also use a stack data structure. You can iterate over collection you wish to reverse by pushing the elements into the stack. Then, when you actually want to use the elements, you pop each element from the stack, which will allow you to iterate over the collection in reverse order.
This solution is working:
for (int i=0; i<v.size (); i++) {
IVec [] vert=v.get(i);
for (int j=0; j<vert.length/2; j++) {
IVec temp = vert[j];
vert[j]=vert[vert.length -1 - j];
vert[vert.length - 1 - j] = temp;
}
}
Try this;
Create a Helper method/function that takes and returns array.
Inside the Helper method, use Collections.reverse
return the reversed array.
call this helper method inside a loop as below:
for(int i = 0; i < OutArray.length; I++)
{ // Here get the inner Array and pass it to Helper method.
// Add the return array to newArray List
}
return newArrayList.
This should work (not the most efficient way, but easily understood):
public static <T> void reverseElements(ArrayList<T[]> list) {
ArrayList<T> tempList = new ArrayList<T>();
for(T[] arr : list) {
tempList.clear();
for(T t : arr)
tempList.add(t);
Collections.reverse(tempList);
tempList.toArray(arr);
arr = tempList.toArray(arr);
}
}
I want to create an array of pointers to linked lists and then go through each list. To create it can I simply create however many lists I need and then just do something like
LinkedList array[] = new LinkedList[length];
and then just set a loop to set each value in the array to point to one of the lists?
How would I go through each list after I set it all up? I thought it was something like
while(array[x].hasNext()){
//do stuff
x++;
}
your while loop is incorrect:
Just do something like this!
for (LinkedList list : array) {
for (Item object : list) {
// Do something here with the item
}
}
btw you should not use LinkedList without a type, use one of the following
LinkedList<String> or
LinkedList<WhateverObjectYouLike> or
LinkedList<? extends whatEverObjectYouLike>
so lets say you want to create an array of 10 lists and every list should contain strings.
LinkedList<String> array[] = new LinkedList<String>[10];
for (int i=0; i<10; i++) {
array[i] = new LinkedList<String>();
}
// Add some strings to each list, as you like
...
// Print all added Strings:
for (LinkedList<String> list : array) {
for (String item : list) {
System.out.println(item);
}
}
You have mixed up Java array objects and the collection framework's List api. You can do what you want in a couple of ways:
Using arrays (highly discouraged—see this thread):
LinkedList array[] = new LinkedList[length];
...
for (int i = 0; i < array.length; ++i) {
LinkedList list = array[i];
// do stuff with list
}
Using a List:
List<LinkedList> array = new ArrayList<LinkedList>();
...
for (Iterator<LinkedList> iter = array.iterator();
iter.hasNext();
)
{
LinkedList list = iter.next();
// do stuff with list
}
In both cases, you might benefit from using an enhanced for loop:
for (LinkedList list : array) {
// do stuff with list
}
This works for either the array-based or List-based versions.
P.S. You should not be using raw LinkedList types. Instead, you should bind the generic type parameter of LinkedList to a specific element type (even if it's Object). For example:
List<LinkedList<String>> array = new ArrayList<LinkedList<String>>();
The language allows raw types for the sake of old code, but new code should never use them. However, the first option above will not work—you will have to use the second approach.