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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:57:32+00:00 2026-05-28T22:57:32+00:00

I’m doing a lab at the moment working on fitness functions. The problem is

  • 0

I’m doing a lab at the moment working on fitness functions. The problem is I’m making a SmallChange() function that does what it says, annoyingly it seems to update the variable you give it, when it shouldn’t.

Here’s a copy of the full class: https://gist.github.com/1710367

Line 38 is the problem line.

And below is the function. When I give it solution as an input it updates solution with the small change it makes, but I can’t figure out how or why.

Anyone know where I’m going wrong? It’s starting to hurt my brain.

// Edits either the angle or velocity of our solution
// by a small amount (about 1% of the diffrence between max and min)
public static Double[] SmallChange(Double[] sol) {
    // Pick a 0 or 1
    switch (r.nextInt(2)) {
    case 1: // Changing the angle
        // The angle change amount
        Double angle = (angleLimits[1] - angleLimits[0]) * 0.01;

        // Make the change, either + or -
        if (r.nextInt(2) == 0) {
            sol[0] += angle;
        } else {
            sol[0] -= angle;
        }

        // Check it's within the limits
        if (sol[0] > angleLimits[1]) {
            sol[0] = angleLimits[1];
        } else if (sol[0] < angleLimits[0]) {
            sol[0] = angleLimits[1];
        }
        break;
    case 0: // Changing the velocity
        // The velocity change amount
        Double velocity = (velocityLimits[1] - velocityLimits[0]) * 0.01;

    // Make the change, either + or -
    if (r.nextInt(2) == 0) {
            sol[1] += velocity;
        } else {
            sol[1] -= velocity;
        }

    // Check it's within the limits
        if (sol[1] > velocityLimits[1]) {
            sol[1] = velocityLimits[1];
        } else if (sol[1] < velocityLimits[0]) {
            sol[1] = velocityLimits[1];
        }
        break;
    }

    return sol;
    }
  • 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-05-28T22:57:34+00:00Added an answer on May 28, 2026 at 10:57 pm

    In Java, everything is passed by value – but that value is always either a primitive, or a reference.

    So the value of any array variable is a reference to an array object. When you use that variable as an argument, the value (the reference) ends up as the initial value of the parameter. It’s still referring to the same array object as the caller’s variable – so any changes made to the array will be seen by the caller.

    So just to clarify, your statement here is incorrect:

    annoyingly it seems to update the variable you give it, when it shouldn’t

    It hasn’t changed the value of the variable at all: that variable in the calling code still has the same value it did before, a reference to the same array. It’s just that the method is changing the contents of the array.

    It’s like copying down your address on a piece of paper, then giving that to someone: they can’t change where you live, but they can change the colour of your front door.


    Now, to fix your problem…

    If you want to clone the array, you’ll have to do so explicitly. For example:

    public static Double[] smallChange(Double[] sol) {
        Double[] ret = (Double[]) sol.clone();
        // Now work on ret instead of sol, and return ret at the end
    

    You could reassign to sol, but personally I wouldn’t.

    Note that you might also want to use a double[] instead of Double[].

    • 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 am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
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
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this

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.