Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:35:42+00:00 2026-06-13T10:35:42+00:00

I call my swap method in main, but it doesn’t change anything. What am

  • 0

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[20];

    for(int i = 0; i<mainArr.length; i++){
    swapper(3, 14, mainArr);
    System.out.print(i + mainArr[i] + " ");
    }
}


public static void swapper (int a, int b, int[] mainArr){
    int t = mainArr[a];
    mainArr[a] = mainArr[b];
    mainArr[b] = t;
}

My code yields

0, 1,  2, 3,...19 

in normal ascending order, where I want it to swap the 4th and 15th element.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:35:43+00:00Added an answer on June 13, 2026 at 10:35 am

    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<mainArr.length; i++){
        mainArr[i] = i;
    }
    
    swapper(3, 14, mainArr);
    
    for(int i = 0; i<mainArr.length; i++){
        System.out.print(i + mainArr[i] + " ");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the most efficient way to swap two params for a method call
Call to undefined method after upgrading to PHP 5.4.0 mentions a few APC bug-reports
Users call a web service API in my PHP code but don't properly encode
I call this function from with my code-behind: DeleteFile(Server.MapPath(/) + sitemap_index.xml) Public Shared Function
I call the following function with a mouseover event but it's not working. My
I call this a flash meeting, but maybe there is another more appropriate name.
I'm attempting to call a method which is outside the class I'm working in
I'm trying to send-retrive message from server by SOAP... but soapConnection.call( soapMessage, endpoint )
I am using Moq - but could easily swap to another mock framework if
I meet this problem when implementing my own swap() method with Python def swap(a,b):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.