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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:01:18+00:00 2026-05-26T11:01:18+00:00

:) The problem I’m having: It’s the very simplified version of what I’m trying

  • 0

🙂

The problem I’m having: It’s the very simplified version of what I’m trying to do, as I actually need to recount a large array of colors (taken from Color Object), and need to sort them, and remove and count duplicates in 2 arrays. (Or whatever data model I should use, I chose 2 arrays).
The problem I’v having is that I cant re-reference the array within a method the way it stays there afterwards.

public class Algos2 {

  private int[] b1 = null;

  public void relink(int[] a1, int[] a2) {
    a2 = a1;
  }

  public Algos2() {
    int[] a1 = { 0, 5, 5, 5, 3, 3, 0, 0, 1, 2, 1, 5};
    relink(a1, b1);
    System.out.println("a1 " + a1); // whatever, working
    System.out.println("b1 " + b1); // still null 
}

  public static void main(String[] args) {
      new Algos2();

  }
}

b1should return the same pointer as a1 afterwards. And I need to do it within a method. :/

Edit: I also can’t do it directly, returning an array by a method. So no: public int[] relink(int[] a1) {return a1;} as I also would neeed to call that from within another method, which also results in null

  • 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-26T11:01:18+00:00Added an answer on May 26, 2026 at 11:01 am

    This doesn’t work because Java passes arguments by value (even if that value is a reference, such as a reference to an array). It can’t change the value of b1 within relink – because all it receives is a copy of the value of b1. (Well, it could assign directly to b1 in this case because it’s an instance method and b1 is an instance variable, but I suspect that’s just a coincidence which may not be valid in your real code.)

    If you want the two variables to refer to the same array, why don’t you just use the assignment operator within the calling method?

    b1 = a1;
    

    Alternatively if you have other work to do, you could potentially make the method return a reference to the appropriate array:

    b1 = relink(a1);
    

    It’s hard to know exactly what’s most appropriate as you haven’t really told us about what you’re trying to do. If you want to be able to remove elements, then arrays may very well be the wrong data structure to use, precisely because you can’t remove elements from them. Have you considered using a List<E> implementation instead, such as ArrayList<E>?

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

Sidebar

Related Questions

Problem: I need an array (datamember) in a parent class A. However, depending on
Problem: I need to retrieve the language of a given cell from the cube.
Problem: I have an address field from an Access database which has been converted
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem: Given a list of strings, find the substring which, if subtracted from the
Problem I have timestamped data, which I need to search based on the timestamp
Problem: We have a web app that calls some web services asynchronously (from the
PROBLEM: Here's the code: import matplotlib.pyplot as plt plt.bar([1,2],[5,4]) plt.title('this is a very long
Problem! I Have the following input (rules) from a flat file (talking about numeric
Problem statement: I want to upload a large binary (such as an audio clip)

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.