Click here to Skip to main content
15,881,172 members
Home / Discussions / Java
   

Java

 
GeneralRe: Which coding language is best for developing web apps? Pin
Richard Deeming14-Dec-22 2:48
mveRichard Deeming14-Dec-22 2:48 
GeneralRe: Which coding language is best for developing web apps? Pin
Richard MacCutchan14-Dec-22 2:58
mveRichard MacCutchan14-Dec-22 2:58 
AnswerRe: Which coding language is best for developing web apps? Pin
Gerry Schmitz14-Dec-22 9:10
mveGerry Schmitz14-Dec-22 9:10 
QuestionPor favor alguien q me ayude con estos ejercicios en netbenas en formulario jframe Pin
Joel Jordan3-Dec-22 13:00
Joel Jordan3-Dec-22 13:00 
AnswerRe: Por favor alguien q me ayude con estos ejercicios en netbenas en formulario jframe Pin
PIEBALDconsult3-Dec-22 13:16
mvePIEBALDconsult3-Dec-22 13:16 
QuestionHow to put the condition if the array may be empty? Pin
Cris29M26-Nov-22 2:37
Cris29M26-Nov-22 2:37 
AnswerRe: How to put the condition if the array may be empty? Pin
Richard MacCutchan26-Nov-22 3:04
mveRichard MacCutchan26-Nov-22 3:04 
GeneralRe: How to put the condition if the array may be empty? Pin
Cris29M26-Nov-22 3:43
Cris29M26-Nov-22 3:43 
"
Why are you trying to access elements of an empty array? 
"

Because is in the statement description. I have some failed tests and I thought maybe because of that.


Note that input array may be empty.




I tried like this, but still it's not working.

Java
<pre>class CycleSwap {
     static void cycleSwap(int[] array) {
        int shift = 1;

        reverse(array, 0, array.length - 1);
        reverse(array, shift, array.length - 1);
    }

     static void cycleSwap(int[] array, int shift) {
         //shift = shift % array.length;
        reverse(array, 0, array.length - 1);
        reverse(array, 0, shift - 1);
        reverse(array, shift, array.length - 1);
    }

    private static void reverse(int[] nums, int i, int j) {
        while (i < j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
            i++;
            j--;
        }
    }

    public static void main(String[] args) {

        {
            int[] array = new int[]{};
            if (array.length == 0) {
                System.out.println(0);
            } else {
                CycleSwap.cycleSwap(array, 2);
                System.out.println(Arrays.toString(array));
            }
        }
     }
}



I get this result:
[ERROR]   CycleSwapTest.testCycleSwapEmptyCaseShift:44 � ArrayIndexOutOfBounds Index 0 o...


How can I fix that?
GeneralRe: How to put the condition if the array may be empty? Pin
Richard MacCutchan26-Nov-22 4:41
mveRichard MacCutchan26-Nov-22 4:41 
GeneralRe: How to put the condition if the array may be empty? Pin
Cris29M26-Nov-22 4:53
Cris29M26-Nov-22 4:53 
QuestionAddress Book System Help Pin
Ghost 717-Nov-22 19:19
Ghost 717-Nov-22 19:19 
SuggestionRe: Address Book System Help Pin
Richard MacCutchan17-Nov-22 20:38
mveRichard MacCutchan17-Nov-22 20:38 
AnswerRe: Address Book System Help Pin
CHill6018-Nov-22 0:02
mveCHill6018-Nov-22 0:02 
QuestionTravel Salesman Problem find itinerary with highest visited sites attractiveness score Pin
Faroug Tifratene16-Nov-22 0:36
Faroug Tifratene16-Nov-22 0:36 
QuestionJavaFX ScrollPane scroll child to bottom Pin
MVSoftVM14-Nov-22 6:30
MVSoftVM14-Nov-22 6:30 
AnswerRe: JavaFX ScrollPane scroll child to bottom Pin
englebart19-Nov-22 15:53
professionalenglebart19-Nov-22 15:53 
QuestionError while fetching Path locally during unit testing but not at runtime Pin
saurabh jasmeen mehta12-Nov-22 19:44
saurabh jasmeen mehta12-Nov-22 19:44 
AnswerRe: Error while fetching Path locally during unit testing but not at runtime Pin
Richard MacCutchan12-Nov-22 21:15
mveRichard MacCutchan12-Nov-22 21:15 
AnswerRe: Error while fetching Path locally during unit testing but not at runtime Pin
jschell14-Nov-22 6:36
jschell14-Nov-22 6:36 
Questionradiusofcircle Pin
Halima Kidiwala9-Nov-22 17:03
Halima Kidiwala9-Nov-22 17:03 
AnswerRe: radiusofcircle Pin
Richard MacCutchan9-Nov-22 22:00
mveRichard MacCutchan9-Nov-22 22:00 
Questioncheck and correct the following code Pin
EngySamy23-Oct-22 14:40
EngySamy23-Oct-22 14:40 
GeneralRe: check and correct the following code Pin
Richard MacCutchan23-Oct-22 21:50
mveRichard MacCutchan23-Oct-22 21:50 
GeneralRe: check and correct the following code Pin
EngySamy23-Oct-22 22:55
EngySamy23-Oct-22 22:55 
GeneralRe: check and correct the following code Pin
Richard MacCutchan23-Oct-22 23:24
mveRichard MacCutchan23-Oct-22 23:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.