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() ) ;
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
I want to convert an array of ints to 1 int.
e.g. I have an array of ints {1,2,3,4,5} and want to convert it to the int 12345
How do I do this?
Iterate through the array, and concatenate the values to String and convert that String to int
String valueSt = "";
for(int val : array) {
valueSt += val;
}
int finalValue = Integer.valueOf(valueSt);
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
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];
}
}
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 set of int Arraylist for my application.For example
int[] array={10,8,-4,-6,5}
Here i want to check this array list.i am using this array for my bar graph chart.if all the value contains positive value,i want to show the graph in 0 to 100 % Y axis.if array contains negative value,i want to show the negative axis like -50 to 100 % depends upon the value.
How to do the logic for this problem.can anyone help me for my application?
boolean isNegative=false;
for(int i=0;i<array.length;i++)
{
if(array[i]<0)
{
isNegative=true;
break;
}
}
if(isNegative)
{
// code for displaying negative axis
}
else
{
//only display positive axis
}