I want an Arraylist in Java, which I want to fill with 10's
ArrayList<Integer> list = new ArrayList<Integer>(100);
for (int i = 0; i < 100; i++) {
list.add(10);
}
I'm going to have to initialize a lot of Arraylists, so I was wondering if there is there a clean way to do this without a for loop?
You can use Collections.nCopies:
ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(100, 10));
This will initialize list with 100 10's.
ArrayList<Integer> list = new ArrayList<Integer>(100);
for (int i = 0; i < list.size(); i++)
{
list.add(10);
}
list.size() will be 0 here, so this is why your code does not work. size keeps track of how many elements are currently in the list, not the capacity.
If you want the ArrayList to be initialized with all 10s, you can use:
ArrayList<Integer> list = new ArrayList<Integer>(Collections.nCopies(100, 10));
Edit: You said later that you didn't want a for loop, but to fix your code, just replace list.size() with 100.
Related
I have created an ArrayList A each element of which further contains an ArrayList. All entries are integers. I wish to then calculate the length of "outer" ArrayList.
ArrayList<Integer>[] A = (ArrayList<Integer>[])new ArrayList[n];
for (int i = 0; i < n; i++) {
A[i] = new ArrayList<Integer>();
}
How can I use the size() or length() functions to obtain the result n?
Directly using A.size() gives the error error: cannot find symbol. This is required because I'm passing A to other functions and do not want to pass n as well every time.
I have created an ArrayList A
Actually, you just created an array of ArrayLists.
A.length gets you the size of that
A[index].size() gets you the sizes of any one list.
If you wanted an ArrayList called A
ArrayList<ArrayList<Integer>> A = new ArrayList<>();
You should use A[i].size() to obtain size of each array list
How can I use the size() or length() functions to obtain the result n?
The identifier A is a reference to an array not ArrayList. The Array A contains ArrayLists.
below shows how to access the length:
ArrayList<Integer>[] A = (ArrayList<Integer>[])new ArrayList[10];
for (int i = 0; i < 10; i++) {
A[i] = new ArrayList<Integer>();
}
System.out.println(A.length);
I am trying to use 2D arrayLists in Java.
I have the definition:
ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
How can I loop through it and enter in numbers starting from 1?
I know that I can access a specific index by using:
myList.get(i).get(j)
Which will get the value. But how do I add to the Matrix?
Thanks
You can use a nested for loop. The i-loop loops through the outer ArrayList and the j-loop loops through each individual ArrayList contained by myList
for (int i = 0; i < myList.size(); i++)
{
for (int j = 0; j < myList.get(i).size(); j++)
{
// do stuff
}
}
Edit: you then fill it by replacing // do stuff with
myList.get(i).add(new Integer(YOUR_VALUE)); // append YOUR_VALUE to end of list
A Note: If the myList is initially unfilled, looping using .size() will not work as you cannot use .get(SOME_INDEX) on an ArrayList containing no indices. You will need to loop from 0 to the number of values you wish to add, create a new list within the first loop, use .add(YOUR_VALUE) to append a new value on each iteration to this new list and then add this new list to myList. See Ken's answer for a perfect example.
Use for-each loop, if you are using Java prior 1.5 version.
for(ArrayList<Integer> row : myList) {
for(Integer intValue : row) {
// access "row" for inside arraylist or "intValue" for integer value.
}
}
Assuming the matrix is not initialized,
int m = 10, n = 10;
ArrayList<ArrayList<Integer>> matrix = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < m; i++) {
List<Integer> row = new ArrayList<Integer>();
for (int j = 0; j < n; j++) {
row.add(j);
}
matrix.add(row);
}
I am trying to add an arraylist in an arraylist in a for loop. When I clear the data in second one, it clears in second arraylist data in first arraylist. Let me explain with code:
ArrayList arrTemp = new ArrayList();
ArrayList arrTemp2 = new ArrayList();
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
//Here I add some strings in arrTemp
}
arrTemp2.add(arrTemp); //I will add arrTemp 10 times with different data in it to arrTemp2
arrTemp.clear(); //Everytime I want to clear the arrTemp
//But when I clear it, it also clears already added arrTemp in arrTemp2
}
I want it to have 10 different arrTemp in arrTemp2
for(int i = 0; i < 10; i++)
{
arrTemp = new ArrayList();
for(int j = 0; j < 10; j++)
.....
You will have different array for each i.
arrTemp2.add(arrTemp);// will add arrTemp object's reference to your arrTemp2.
So, once you clear arrTemp, you actually will clear() the space being pointed by arrTemp.
use Generics with addAll(), like this:
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(1);
al.add(2);
ArrayList<Integer> al2 = new ArrayList<Integer>();
al2.add(3);
al.addAll(al2);
al2.clear();
System.out.println(al);
}
O/P :
[1, 2, 3]
Your case :
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(1);
al.add(2);
ArrayList al2 = new ArrayList();
al2.add(3);
al.add(al2);
al2.clear();
System.out.println(al);
}
O/P :
[1, 2, []] // 3rd item is considered as an Object
I'm not positive, but it seems to me that your addTemp2.add(arrTemp) is not working as intended. Have you tried debugging and checking whether arrTemp2 is actually being populated with something after that line fires? Looking at the documentation (below) for ArrayLists, I think you might want addAll instead. Alternately, you could iterate over arrTemp and add each element individually to arrTemp2 before you clear arrTemp.
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Easy..
Replace
arrTemp.clear(); with arrTemp = new ArrayList();
I have these three collections in Java:
ArrayList<Integer> list1 = new ArrayList<Integer>(6000);
ArrayList<Integer> list2 = new ArrayList<Integer>(6000);
ArrayList<Integer> list3 = new ArrayList<Integer>(6000);
Which is more efficient to loop through and set to 0?
for(int i =0; i < rHist.size(); i++) {
list1.set[i] = 0;
list2.set[i] = 0;
list3.set[i] = 0;
}
Or this?
for (int n : list1) {
n = 0;
}
for (int n : list2) {
n = 0;
}
for (int n : list3) {
n = 0;
}
Thanks
As already mentioned in the comments, both of the variants do not reach the goal you stated. That being said, better look to write readable, understandable and short code and refrain to the methods given in the Collections API. Something like that fills your list with 6000 copies of zeros:
List<Integer> list = Collections.nCopies(6000, 0);
If you need to mutate the list afterwards, you need to wrap it in a modifiable List like so:
List<Integer> list = new ArrayList<Integer>(Collections.nCopies(6000, 0));
The first one is better because you are looping one time instead of three if you put the size to 6000 in the loop, and the set should look like this list1.set(i,0) instead of list1[i]=0
if you decompiled both them uisng javap -c "appname" lets assume that they are under the main, you can see your self whats happening
List<Double> elements = new ArrayList<Double>();
List<Integer> usedElements = new ArrayList<Integer>();
for (int i=0; i<usedElements.size(); i++)
{
elements.remove(usedElements.get(i));
}
There is a List of indexes called usedElements. I need to delete those elements from the List elements that are mentioned in usedElements. How to do this in a correct way.
If you know your usedElements list is in ascending order, the simplest approach would be to remove the elements in reverse order - that way the "shuffling up" effect won't affect any of the later operations:
List<Double> elements = ...;
List<Integer> usedElements = ...;
for (int i = usedElements.size() - 1; i >= 0; i--) {
elements.remove(usedElements.get(i));
}
If usedElements isn't currently sorted, it would be best to just sort it first. If usedElements isn't currently sorted and you need to maintain its current order for another reason, then create a copy first, and sort that:
List<Double> elements = ...;
List<Integer> usedElements = ...;
List<Integer> sortedUsedElements = new ArrayList<Integer>(usedElements);
Collections.sort(sortedUsedElements);
for (int i = sortedUsedElements.size() - 1; i >= 0; i--) {
elements.remove(sortedUsedElements.get(i));
}
Or even sort the copy in reverse order and use an enhanced for loop:
List<Double> elements = ...;
List<Integer> usedElements = ...;
List<Integer> sortedUsedElements = new ArrayList<Integer>(usedElements);
Collections.sort(sortedUsedElements, Collections.<Integer>reverseOrder());
for (Integer index : sortedUsedElements) {
elements.remove(index);
}
You might find it easier to create a new list instead of trying to modify the original list in place:
Set<Integer> used = new HashSet<>(usedElements); // maybe use a set in the
// first place?
List<Integer> newElements =
new ArrayList<>(elements.size() - used.size());
for (int i = 0; i < elements.size(); i++) {
if (!used.contains(i))
newElements.add(elements.get(i));
}
elements = newElements;
This entire process is O(n).