Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an ArrayList and I want to start iterating through it from, say, index 100 to the end. How do I do that?
There are many ways to do this. In this examples I assume your list holds Integers.
You can use ListIterator
ListIterator<Integer> it = list.listIterator(100);
while (it.hasNext()) {
System.out.println(it.next());
}
or with for (to keep iterator scoped inside loop)
for (ListIterator<Integer> lit = list.listIterator(100); lit.hasNext();) {
System.out.println(lit.next());
}
or normal for loop but start from i=100
for (int i=100; i<list.size(); i++){
System.out.println(list.get(i));
}
or just create subList and iterate over it like you normally do
for (Integer i : list.subList(100, list.size())){
System.out.println(i);
}
You can always utilize the subList(int, int) method
list.subList(100,list.size()).iterator();
Try like this to iterate over a range:
for(int i=100; i< myArrayLst.size(); i++){
System.out.println(myLst.get(i));
}
Use a ListIterator. And read the API documentation, thank you.
ListIterator<YourType> iter = list.listIterator(start);
Use ListIterator
ListIterator<Type> iter = list.listIterator(start);
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to go through a stack and a queue and print out the Object's values. In my Object's class I have implemented a toString. My stack and queue classes use a linked list. I tried going through it with a for loop like an array first, obviously doesn't work. I know how to get the top but not all of them.
for (i = 0; i <= 9; i++) {
System.out.println(storageA[i].toString());
}
storageA is my stack with fixed size of 10.
Assuming you want to iterate over the complete list holding Objects of type E:
for (E element : storageA) {
System.out.println(element.toString());
}
If you really only want elements 0-9 (better validate storageA.size > 9, else you get an IndexOutOfBoundsException):
for (i = 0; i <= 9; i++) {
System.out.println(storageA.get(i).toString());
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help me for below situation?
List ll = new LinkedList();
ll.add(10);
ll.add(20);
ll.add("king");
ll.add(30);
ll.add(40);
I want the sum of all above integers in list.
Just iterate over the list and for each element check if it's an integer or not:
int sum = 0;
for (Object o : ll) {
if (o instanceof Integer) {
sum += ((Integer)o).intValue();
}
}
Seems like a homework question to me hence I have voted it for closing. But I thought I can still give you some tit bits that will help you.
Generics in Java are your friends.
Firstly use Generics wherever possible.
If you know that the List is only going to contain Integers, then declare it like
List<Integer> list = new LinkedList<Integer>();
Regarding Sum:
Just take a variable sum, iterate over it using for each loop and add all the elements by casting it to Integer if the type is instanceof Integer. Mind that you wont need this instanceof check if you declare it like I suggested. Assuming you only will have integers in list. Otherwise declare it like List<Object> list = new LinkedList<Object>();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I don't know what this loop does. Can anybody tell me?
for (String Number : Num) {
lineIndex[count] = Integer.parseInt(Number);
count++;
}
It's a for-each loop.
Num is some object which implements Iterable<String> like a String[] array or a collection like for example ArrayList<String>.
The loop is executed once for each entry in that data structure. In each iteration of the loop, String Number is a different entry.
What the loop does exactly, is to read each entry from Num, parse it as an integer, and put it into the lineIndex array.
This is a for-each loop, where Num is a collection of String and Number is the current element in the collection in each iteration.
In the loop body, it is just assigning the parsed string to an element in lineIndex array, and incrementing count.
It is equivalent to:
for (int i=0; i<Num.Length; i++)
{
String Number = Num[i];
lineIndex[count] = Integer.parseInt(Number);
count++;
}
I think you Should read Core java 2. Volume I, Fundamentals or java effective.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an ArrayList and I am trying to measure the time it takes to retrieve an item from the middle of my ArrayList.
Here it is:
List<Car> cars = new ArrayList<Car>();
for (int i = 0; i < 1000000; i++) {
cars.add(new Car(null, i));
How would I retrieve item 500000?
I tried creating a variable like int example = 500000, then put cars.get(example). But I just got errors:
java.lang.IndexOutOfBoundsException: Index: 500000, Size: 1
Here why am I getting an IndexOutOfBoundsException even when the index I requested < total entries?
Any help would be appreciated. Thanks.
Use the get() function
cars.get(500000);
EDIT
IndexOutOfBoundsException means you are trying to retrieve an information that is beyond the list.
Your error is not related to the function you're using, you are probably making a mistake somewhere else. Please realize whether you're populating and trying to read from the same variable.
Your list is named car, but you are adding to a list named cars.
If you want to create an indexed collection of data you would likely want to use something other than an ArrayList.... consider a HashMap for example:
HashMap<Integer, Car> map = new HashMap<>();
for (int i = 0; i < 1000000; i++) {
map.put(i, new Car(null, i));
}
Car c = map.get(500000);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I'm trying to learn algorithm efficiency. I know how to remove one element from the array, but not sure how to remove between two indices. Lets pretend that the
list = {1,2,3,4,5,6,7,8,9,10}, and we call the removeBetween method with arguments:
removeBetween(2, 6);
public void removeBetween(int FirstIndex, int LastIndex)
{
}
A general algorithmic direction you can follow :
To remove all numbers between 2 given indices, say (FirstIndex, LastIndex):
Copy all elements from index 0 to FirstIndex to a result array.
Next copy all elements from LastIndex to Array.lenght()-1 indices to the same result array above.
Return result.
so if you have your items in an array list you can do something like:
ArrayList<Whatever object is> newElements = yourlist.sublist( 0, firstIndex );
newElements.addAll( yourlist.subList( LastIndex + 1, yourlist.size() ) ;