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
Related
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++;
}
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 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
else if occur the case of
ArrayList of Integer ArrayList and I want remove only one index inside the ArrayList?
For Example:
1 -> [3,1,8,9]
ArrayList 2 -> [0,2,4,5]
3 -> [9,3,4,5,5,9,0]
I wish remove the first index of ArrayList 2 that is the zero (0).
If I use
ArrayList.remove(2), it will delete all first position. That is [0,2,4,5]. But I just want to remove the first position. The number 0.
How to proceed?
In this case, my block code is:
for (int i = 0; i < conta.size(); i++)
{
listaRepetida2[conta[i]].remove(conta[i+1]);
listaRepetida2[conta[i]].remove(conta[i+1]);
/*
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
*/
i+=2;
}
conta, get the index of listaRepetida[i][j] that will be remove from it.
You get the inner list which is at index 1:
List<Integer> innerList = outerList.get(1);
And then you remove its first element:
innerList.remove(0);
Or, in a single instruction:
outerList.get(1).remove(0);
First you should fetch the array you want to remove elements from. You an simply use onstruct like this:
arr.get(2).remove(0);
See it;
My code (don't remove):
import java.util.ArrayList;
public class RemoveRepetidas
{
ArrayList<ArrayList<Integer>> listaRepetida = new ArrayList<ArrayList<Integer>>();
RemoveRepetidas (ArrayList<ArrayList<Integer>> _listaRepetida)
{
this.listaRepetida = _listaRepetida;
}
String removeRepetida()
{
System.out.println(this.listaRepetida);
ArrayList<Integer> conta = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> listaRepetida2 = listaRepetida;
for (int i = 0; i < listaRepetida2.size(); i++)
{
for (int j = i+1; j < listaRepetida2.size(); j++)
{
for (int k = 0; k < listaRepetida2.get(i).size(); k++)
{
for (int l = 0; l < listaRepetida2.get(j).size(); l++)
{
if (
( listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l+1) ) ||
( listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l+1) )
)
{
conta.add(j);
conta.add(l);
}
l++;
}
k++;
}
}
}
for (int i = 0; i < conta.size(); i++)
{
//This does not remove
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
i+=2;
}
String texto = "{";
texto += "}";
System.out.println(listaRepetida2);
return texto;
}
}
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 9 years ago.
Improve this question
Hi I think there may be a problem with my 3 way sort algorithm in the following Java program, also any suggestion on optimizing or just a simpler it would be greatly appreciated. The objective of the sort is to have the minus numbers first then zeros and then positive numbers
class ThreeWaySort
{
public static void main(String[] args)
{
int location = 0;
int[] sArray = new int[50];
for (int a = 25; a<= -24; a--)
{
sArray[location] = a;
location++;
}
int i = 0; int j = 0; int k = 50;
while (j!=k)
{
if (sArray[j]==0)
{
j++;
}
else if (sArray[j]<0)
{
int t = sArray[i]; sArray[i] = sArray[j]; sArray[j] = t; // case (ii)
i++; j++;
}
else
{
k--;
int t= sArray[j]; sArray[j] = sArray[k]; sArray[k] = t;
}
}
for (int a = 0; a <= 49; a++)
{
if(sArray[a] >-1)
{
System.out.println();
System.out.println();
System.out.println();
}
if(sArray[a] > 0)
{
System.out.println();
System.out.println();
System.out.println();
}
System.out.print(sArray[a] + " ");
}
}
}
When i run the program as is it costantly print out a zero followed by three line instead of what I'm expecting to be, Numbers below zero in a line, followed by 3 blank lines then any zeros in the array, 3 blank lines, positive numbers in the array.
The loop that populates your array is incorrect:
for (int a = 25; a<= -24; a--)
The variable a starts at 25, which is not less than or equal to -24, so the loop never executes. You should use >=.