How do I swap two integers in an array, where my method takes in two integers and an array from main?

后端 未结 4 742
既然无缘
既然无缘 2020-12-12 06:17

I call my swap method in main, but it doesn\'t change anything. What am I doing wrong?

public static void main(String[] args){


    int mainArr[] = new int[         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 06:45

    Move the method call: -

    swapper(3, 14, mainArr);
    

    outside your for loop. Since, if your loop runs even number of times, it will not affect the array.

    Also, you need to initialize your array first, before actually swapping the elements. That you would need to do before invoking swapper.

    for(int i = 0; i

提交回复
热议问题