Retrieving integer values from LinkedList [closed] - java

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>();

Related

Remove elements from arraylist based on a condition [closed]

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 whose each element is of type DataType, where DataType is a class:
class DataType{
String dId;
String dType;
String rId;
}
I need to remove all such elements from the list whose rId is equal to any other element's dID.
i.e. if DataType D1 has value of dID as "abc" and DataType D2 has value of rID as "abc", than remove both D1 and D2 from the list.
Could someone please suggest the most appropriate approach for doing this.
The easiest would be to traverse the list once and create a HashMap<String, List<DataType>>.
You will map every object to their dID which forms the primary key.
After that you can iterate over your ArrayList, check the rId of the current object and see if it's in the HashMap. HashMap has O(1) lookup time so this should be a non issue. If the value is present, remove the current value (you're using an Iterator to prevent a ConcurrentModificationException) and remove the objects inside the value-part of the key-value pair as well.
Make sure you have correctly implemented .equals(Object o) and .hashcode().

How to start iterating through ArrayList from set index? [closed]

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);

can any one give me the answer about this java loop [closed]

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.

Retrieve Nth item from ArrayList gives IndexOutOfBoundsException even when index is in range? [closed]

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);

Best algorithm to remove elements between 2 given indices [closed]

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() ) ;

Categories

Resources