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 8933977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:40:41+00:00 2026-06-15T09:40:41+00:00

I’m trying to shift all values in an array with a given number. For

  • 0

I’m trying to shift all values in an array with a given number. For example array {1,2,3,4,5} with 1 shift has to become {5,1,2,3,4}. This is tested with JUnit.

The JUnit test is this:

    @Test    
    public void shift1(){
        double[] row = {1.0,2.0,3.0,4.0,5.0};
        int amount= 1;
        ArrayOperations.shift(row, amount);
        Assert.assertEquals(5.0, row [0]);
        Assert.assertEquals(1.0, row [1]);
        Assert.assertEquals(2.0, row [2]);
        Assert.assertEquals(3.0, row [3]);
        Assert.assertEquals(4.0, row [4]);      
    }

My method is this:

public static void shift(double[] row, int amount) {
    double[] newRow= new double[row.length];
    for (int i = 0; i < newRow.length; i++) {
        newRow[(i + amount) % row.length] = row[i];
    }

    row= newRow;
}

Now this test fails for any unknown reason. I had this problem in programming class and even my teacher didn’t find out why. When I debug this, the array is modified correctly resulting in {5,1,2,3,4}. But the JUnit fails… This code however works:

public static void shift(double[] row, int amount) {
    double[] newRow= new double[row.length];
    for (int i = 0; i < newRow.length; i++) {
        newRow[(i + amount) % row.length] = row[i];
    }

    for (int i = 0; i < newRow.length; i++) {
        row[i] = newRow[i];            
    }
}

Anyone know why? row= newRow; is correct right? Even the debugger says it’s changed correctly… So why does the test fail with the first implementation, but not with the second?

  • 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-15T09:40:42+00:00Added an answer on June 15, 2026 at 9:40 am

    Now this test fails for any unknown reason

    No, it doesn’t work because you’re confused how parameters work in Java. This has nothing to do with JUnit, or arrays specifically. It has everything to do with parameters.

    row= newRow; is correct right?

    No, it’s not. That just sets the value of the parameter, which is just a local variable within the method. It does nothing to the caller’s variable, which is what you were expecting.

    A simpler example:

    String x = "foo";
    method(x);
    System.out.println(x); // Still prints foo...
    
    ...
    static void method(String p) {
        p = "bar";
    }
    

    All arguments are passed by value in Java… although you need to understand that for reference types, those values are references (not objects).

    I suggest you find a good Java tutorial or book, and read up more on parameter passing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.