can any one give me the answer about this java loop [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
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.

Related

How come for Integer arrays, as in Integer arr[] = new Integer[], you can't change their value, you can only set them? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I was doing a problem in Codeforces and had to use an Integer arr[] = new Integer[200001]. I wanted to store 200001 counters for their index values(for example, if I got 19 then arr[19] would be the number of 19s that appeared in the array). However, when I went to do arr[num]++ or arr[num] += 1, it would not work. Only when you set arr[num] to some number does it work. Is there a way to get around that?
It wouldn't work because you haven't initialized each element of the array, you just have an Integer array with null references.
you should first do:
for(int i = 0; i < arr.length; i++) {
arr[i] = 0;
}
The easiest way is to use an int array instead of an Integer array.

Retrieving integer values from LinkedList [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
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>();

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

how to find max 2 element in 10 element less complexity in java? [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
can you please tell me how to find max 2 element in 10 element less complexity in java .
I do like this but it complexity is too high .i need to reduce that
Algo
take max =a[0]
for(int i =0;i<10;i++){
if(a[i]>max)
max=a[i]
}
Second way to sort the array using bubble sort then find the last 2 element ?
Instead of sorting the array, you can simply do the following:
Keep a largestValue and a secondLargestValue
Loop through the entire array once, for each element:
Check to see if the current element is greater than largestValue:
If so, assign largestValue to secondLargestValue, then assign the current element to largestValue (think of it as shifting everything down by 1)
If not, check to see if the current element is greater than secondLargestValue
If so, assign the current element to secondLargestValue
If not, do nothing.
O(n) run time
O(1) space requirement
max =a[0];
max2 = a[0];
for(int i =0;i<10;i++)
{
if(a[i]>max)
{
max2=max;
max=a[i];
continue;
}
if(a[i]>max2||max2==max)
{
max2=a[i];
}
}

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