Click here to Skip to main content
15,561,948 members
Home / Discussions / Java
   

Java

 
QuestionWhich coding language is best for developing web apps? Pin
nithin sethu14-Dec-22 3:18
nithin sethu14-Dec-22 3:18 
AnswerRe: Which coding language is best for developing web apps? Pin
Richard MacCutchan14-Dec-22 3:41
mveRichard MacCutchan14-Dec-22 3:41 
GeneralRe: Which coding language is best for developing web apps? Pin
Richard Deeming14-Dec-22 3:48
mveRichard Deeming14-Dec-22 3:48 
GeneralRe: Which coding language is best for developing web apps? Pin
Richard MacCutchan14-Dec-22 3:58
mveRichard MacCutchan14-Dec-22 3:58 
AnswerRe: Which coding language is best for developing web apps? Pin
Gerry Schmitz14-Dec-22 10:10
mveGerry Schmitz14-Dec-22 10:10 
QuestionPor favor alguien q me ayude con estos ejercicios en netbenas en formulario jframe Pin
Joel Jordan3-Dec-22 14:00
Joel Jordan3-Dec-22 14:00 
AnswerRe: Por favor alguien q me ayude con estos ejercicios en netbenas en formulario jframe Pin
PIEBALDconsult3-Dec-22 14:16
professionalPIEBALDconsult3-Dec-22 14:16 
QuestionHow to put the condition if the array may be empty? Pin
Cris29M26-Nov-22 3:37
Cris29M26-Nov-22 3:37 
I have this problem where I solved it  when my array is not empty.
My issue is whem the array is empty.  I tried to put an condition but it's useless.
I got this error "[ERROR]   CycleSwapTest.testCycleSwapEmptyCaseShift:44 � ArrayIndexOutOfBounds Index 0 o..."

Proceed to CycleSwap class and implement its static methods:

void cycleSwap(int[] array)
Shifts all the elements in the given array to the right by 1 position.
In this case, the last array element becomes first.
For example, 1 3 2 7 4 becomes 4 1 3 2 7.

void cycleSwap(int[] array, int shift)
Shift all the elements in the given array to the right in the cycle manner by shift positions.
Shift value is guaranteed to be non-negative and not bigger than the array length.
For example, 1 3 2 7 4 with a shift of 3 becomes 2 7 4 1 3.
For a greater challenge, try not using loops in your code (not mandatory).

Note that input array may be empty.


Examples
Code Sample:

int[] array = new int[]{ 1, 3, 2, 7, 4 };
CycleSwap.cycleSwap(array);
System.out.println(Arrays.toString(array));
Output:

[4, 1, 3, 2, 7]


Code Sample:

int[] array = new int[]{ 1, 3, 2, 7, 4 };
CycleSwap.cycleSwap(array, 2);
System.out.println(Arrays.toString(array));
Output:

[7, 4, 1, 3, 2]


Code Sample:

int[] array = new int[]{ 1, 3, 2, 7, 4 };
CycleSwap.cycleSwap(array, 5);
System.out.println(Arrays.toString(array));
Output:

[1, 3, 2, 7, 4]

My code is:


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[]{1, 3, 2, 7, 4};
            CycleSwap.cycleSwap(array,2);
            System.out.println(Arrays.toString(array));
        }
        {
            int[] array = new int[]{};                                                        // I tried here to put this condition: if (array == null) { 
            CycleSwap.cycleSwap(array);                                               //                                                           System.out.println("0");
            System.out.println(Arrays.toString(array));                         //                                                        }
        }
        {
            int[] array = new int[]{};                                                          // here the same condition from above
            CycleSwap.cycleSwap(array, 3);
            System.out.println(Arrays.toString(array));
        }
 
 
     }
}
 
Output: 
[7, 4, 1, 3, 2]
[]
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
    at com.epam.rd.autotasks.CycleSwap.reverse(CycleSwap.java:22)
    at com.epam.rd.autotasks.CycleSwap.cycleSwap(CycleSwap.java:16)
    at com.epam.rd.autotasks.CycleSwap.main(CycleSwap.java:43)




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

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.