Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
just getting into java.
What is the best way to encounter this issue?
I understand what is happening, and I am asking for the best way to solve it, cause my solution is a bit ugly I might say.
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if(c.get(i+2)==0)i++; //Last 2 iteration will gives the error.
}
return jumpx;
}
My newbie solution
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if (!(i+1>=size || i+2>=size)){ //adding this to avoid c.get(i+2) to be executed.
if(c.get(i+2)==0)i++;
}
if(i+2>=size || i+1>=size)break; //adding this to avoid jumpx++ to be executed.
}
return jumpx;
}
I might be a bit blur, but I hope someone can explain and shows the best one (with the code).
Thank you.
You can validate the i + 2 value to be inside the range before trying to perform c.get(i+2) to avoid the exception
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; i<size; i++){
jumpx++;
if(i+2 <size && c.get(i+2)==0)i++;
}
return jumpx;
}
A simpler way to modify the original code (without encountering an IndexOutOfBoundsException) would be:
public static int jumpingOnClouds(List<Integer> c) {
int size = c.size();
int jumpx = 0;
for(int i = 0; (i + 2) < size; i++){
jumpx++;
if(c.get(i+2)==0)i++;
}
return jumpx;
}
Since you are modifying i, and need to access from the (i + 2)-th index, you need to ensure that (i + 2) < size, since size - 1 is the last valid index in the List
// here you are looping through all elements in the list
for(int i = 0; i<size; i++){
jumpx++;
//here you are getting the "i + 2"th element.
//This is going to be out of bounds if i is the 2nd
//to last or the last element
if(c.get(i+2)==0)i++;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I was scrolling through edabit doing some of their challenges when I stumbled across one where you were to return the amount of prime numbers under the passed value. I completed the challenge and wanted to check if my solution was quicker than most others, so I checked one and found out that my code, according to the program at least, was slower even though mine is supposed to do fewer checks.
Here is the code I wrote:
public static int test1(int num) {
int primes = 0;
for(int i = 3; i<=num; i+=2) {
boolean prime = true;
for(int j = (int) Math.sqrt(i); j>1; j--) {
if(i%j==0) {
prime = false;
break;
}
}
if(prime == true) primes++;
}
return (num >= 2 ? primes+1 : 0);
}
and here is the other one:
public static int test2(int num) {
int result = 0;
for (int i = 2; i <= num; i++) {
int count = 0;
for (int j = 1; j <= i; j++){
if (i % j == 0){
count++;
}
}
if (count == 2){
result++;
}
}
return result;
}
By a quick look over you can see that mine is almost the same as the other code sample but optimized as it is supposed to do fewer checks as the number of times it will loop is less. However, that is apparently not the case. Could someone please tell me why my code would not be running as fast or faster than the other code sample? I only tested values between 1 and 150 in case that information is useful.
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 5 years ago.
Improve this question
I have this code which suppose to do the following task:
For example , given M=3, and array built like this:
A[0]=1
A[1]=1
A[2]=3
A[3]=3
A[4]=5
A[5]=1
A[6]=3
the function may return 1 or 3
import java.util.*;
class Solution {
int solution(int M, int[] A) {
int N = A.length;
int[] count = new int[M + 1];
for (int i = 0; i <= M; i++)
count[i] = 0;
int maxOccurence = 1;
int index = -1;
for (int i = 0; i < N; i++) {
if (count[A[i]] > 0) {
int tmp = count[A[i]];
if (tmp > maxOccurence) {
maxOccurence = tmp;
index = i;
}
count[A[i]] = tmp + 1;
} else {
count[A[i]] = 1;
}
}
return A[index];
}
}
what could be the problem because it is not always working and I can see there is a bug in my program.
1 1 1 1 5 5 5 5 5 this is a case where your code may fail. Check and update the max occurance variable outside the loop too. Above case give enough justice to my point, i hope.
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 8 years ago.
Improve this question
It prints
Flip Method (only prints 9 to 5 ) but I want it to print from 9 to 0
http://imgur.com/gggiJwn
public static void flip(int[] flp){
System.out.println("This is the flip method");
for ( int i = 0; i < flp.length; i++){
int e = flp.length-1;
int temp = flp[e-i];
flp[i] = flp[e-i];
flp[i] = temp;
e--;
System.out.println("Index"+(i)+" :"+flp[i]); //is this the problem?
}
}
}
You need to change your logic to following to reverse the full array
public static void flip(int[] flp){
System.out.println("This is the flip method");
for ( int i = 0; i < flp.length/2; i++){
int e = flp.length-(1+i);
int temp = flp[i];
flp[i] = flp[e];
flp[e] = temp;
// remove print from here. else you will get half of the array
// since flp.length/2
}
}
Add that in a separate method.
for(int i = 0; i < flp.length; i++){
System.out.println("Index"+(i)+" :"+flp[i]);
}
The problem is that you are printing half of the elements
i < flp.length/2
That should be
i < flp.length
Actually, your for loop is fine - but you should remove the printing from inside that loop, and have it in a seperate loop after the flip loop
for ( int i = 0; i < flp.length; i++){
System.out.println("Index"+(i)+" :"+flp[i]);
}
public static void flip(int[] flp) {
System.out.println("This is the flip method");
int e = flp.length - 1;
for (int i = 0; i < flp.length / 2; i++) {
int temp = flp[i];
flp[i] = flp[e];
flp[e] = temp;
e--;
}
for (int i = 0; i < flp.length; i++) {
System.out.println("Index" + (i) + " :" + flp[i]);
}
}
Your for loop prints 9 to 5 because this is how you have configured your loop to iterate till i < flp.length/2 .
Change it to:
i < flp.length
from
i < flp.length/2
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 8 years ago.
Improve this question
I have written this java program for sorting some numbers. But it doesn't stop executing. Can you help me with it?
package inplacesort;
import java.util.*;
public class InplaceSort
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
Vector <Integer> intList = new Vector <Integer> ();
//getting the numbers from the user
char ans = 'y';
while (ans == 'Y' || ans == 'y')
{
System.out.print("Enter a Number: ");
intList.addElement(console.nextInt());
System.out.print("Do You Want to Continue?(Y/N)");
ans = console.next().charAt(0);
}
System.out.println("Before Sorting the Numbers: " + intList);
for (int i = 1; i < intList.size(); i++)
{
int j = i - 1;
while (j > 0 && intList.elementAt(i) < intList.elementAt(j))
{
j--;
}
for (int k = intList.size() - 1; k >= j; k--)
{
intList.insertElementAt(intList.elementAt(k),k + 1);
}
intList.insertElementAt(intList.elementAt(i+1),j);
intList.removeElementAt(i+1);
}
System.out.print(intList);
}
}
Your problem is with intList.size() in the k & i loops. This is not be as what you would expect it. When I debugged your code the value of k was 425996.
Edit :
When i debugged it more I saw that because of you mutating the vector within it self it keeps increasing in size. If you let your program run for a few minutes you will get out of memory error.
Please don't mutate the object you are looping though it. Either make a copy of it and the loop though one of them and mutate another or start with a fresh one & keep adding the values to it while looping over the older one.
System.out.println("Before Sorting the Numbers: " + intList);
List<Integer> sortList = new ArrayList<Integer>();
int minVal;
int index=0;
int size = intList.size();
for (int i = 0; i < size; i++)
{ minVal=Integer.MAX_VALUE;
for (int j = 0; j < intList.size(); j++)
{
if(intList.get(j) < minVal){
index=j;
minVal=intList.get(j);
}
}
intList.remove(index);
sortList.add(minVal);
}
System.out.print("After Sorting the Numbers: "+ sortList);
The reason is because your value for j is ALWAYS 1 less than the value for i. Therefore, infinite while loop.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to make a game in java but have run into what seems like a simple problem. I need some way to use a loop to print out multiple things but not the traditional way. Basically what i need to do is this:
instead of:
for(int i=0;i<5;i++)
{
e.get(i);
}
i need to do this:
for(int i=0;i<5;i++)
{
e.get(0);
e.get(1); //but 1 and above can only be there after a number has been increased past 0
e.get(2);
e.get(3);
e.get(4);
}
where changing i would also change how many "e.get()"s you have.
Any ideas?
to clear things up:
this will not work:
public static void main(String[] args)
{
int l=5;
for(int i=0;i<l;i++)
{
for(int o=0;o<l;o++)
{
e.get(o);
}
}
}
but something along the lines of this will:
public static void main(String[] args)
{
e.get(0);
e.get(1); //but 1 and above can only be there after a number has been increased past 0
e.get(2);
e.get(3);
e.get(4);
}
I have tried the nested for-loop but it does not work for my program.
for my program to work, each "e.get(0);" needs to physically be there.
sorry if im making this unclear, i have been programming for like 6 hours straight and am reaching a wall :/
int num = 5;
for (int i=0; i<num; i++)
for (int j=0; j<num; j++)
e.get(j);
Edit:
Do you mean this?
int num = 5;
e.get(0);
for (int i=1; i<num; i++)
for (int j=0; j<num; j++)
e.get(j);
for(int i = 0; i < 5; i++)
{
for (int j = 0; j < i; j++)
{
e.get(j);
}
}
Something like this?
I would try e.length;
for(int i=0;i<e.length;i++)
{
e.get(i);
}
So something like this?
for (int i = 0; i < 5; i++) {
e.get(0);
if (i > 0)
e.get(1); //but 1 and above can only be there after a number has been increased past 0
if (i > 1)
e.get(2);
if (i > 2)
e.get(3);
if (i > 3)
e.get(4);
}
That would make certain all of the e.get() calls are in your code, and I believe would call them correctly if I understand what you want (which I probably don't!).