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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:16:45+00:00 2026-06-02T16:16:45+00:00

I have two questions: First: I have a function which returns a HashMap. To

  • 0

I have two questions:
First:
I have a function which returns a HashMap. To read the returned value, I write it like this:

    HashMap<Integer,String> hs=my_func2();

I do the same if the function returns a HashSet.

    HashSet<Integer> hs=my_func();

I wanted to know if in this way the returned value is copied into hs, or I should write a deep copy for it or I should write it like this:
HashSet hs=new HashSet(my_func());
HashMap hm=new HashMap(my_func2());

Second quesion:
I make a matrix by calling make_matrix_funciton. matrix woule be a 2-dimensional array containing:
[0 1 1
0 0 0
0 0 0]
Then I give this matrix to sort_vec, and in this function the elements of matrix change. I think java is not pointer based, so when I come out of sort_vec, matrix should be as it had been. But, it has changed! It is
[0 0 0
0 0 0
1 1 0]
which shows the changes that had been applied to it inside the sort_vec function. Is it normal and if yes, what should I do to prevent it. The code below is compilable.

public static void main(String args[]) {
        int matrix[][]=new int[3][3];
        matrix=make_matrix("011000000");
        int indexes[]={2,1,0};
        int[][] mat=sort_vec(3,matrix,indexes);
    }

    private static int[][] sort_vec(int motifsize,int [][]mat,int[] indexes)
    {
        int[] main_index={0,1,2};

        int l=indexes.length;
        for (Integer i=0;i<l;i++)
            if(indexes[i]!=main_index[i])
            {
                int j=indexes[i];
                int k=main_index[i+1];
                for(;k<l;k++)
                    if(indexes[k]==main_index[i])
                        break;

                indexes[k]=j;
                mat=exchange(motifsize,mat,j,main_index[i]);
            }
        return mat;
    }
    private static int[][] exchange(int motifsize,int [][]matrix,int x,int y)
    {
        int temp;

        for(int i=0;i<motifsize;i++)
        {   
            temp=matrix[i][x];
            matrix[i][x]=matrix[i][y];
            matrix[i][y]=temp;
        }
        for(int i=0;i<motifsize;i++)
        {   
            temp=matrix[x][i];
            matrix[x][i]=matrix[y][i];
            matrix[y][i]=temp;
        }

        return matrix;
    }
    private static int[][] make_matrix(String id)
    {
        int matrix[][]=new int[3][3];
        int c=0;
        for(int x=0;x<3;x++)
            for(int y=0;y<3;y++)
            {
                if(id.charAt(c)=='1' || id.charAt(c)=='5')
                    matrix[x][y]=1;
                c++;
            }
        return matrix;
    }
  • 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-02T16:16:47+00:00Added an answer on June 2, 2026 at 4:16 pm

    Java always passes Objects by reference, so if you return a HashMap-Object from a function, the reference will be passed to the hs variable in your example. Passing the HashSet to the constructor of a new HashSet instance will not work. It will create a new HashSet with the same object references as in the original one. If you modify one of these objects, the change will appear on all other reference points, too.

    If you want to totally detach the copy, you will need your own method for deep copying because in the JavaDoc for the clone() method it says:

    Returns a shallow copy of this HashSet instance: the elements themselves are not cloned.

    The same goes for arrays. Every array is an object, so if you modify an element, it will be modified for all references to this array. To create a deferred copy, use System.arrayCopy

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

Sidebar

Related Questions

I have two questions, actaully... First off, Why cant I do this: List<Object> object
I have two questions. (First) I'm trying to make it where whenever the iPhone
Hey everyone, I want to start using Scheme and I have two questions. First,
I am calling a function which returns a string containing XML data. How this
I have two objects defined something like this (simplified for sake of the question):
Background I have a project named PersonLibrary which has two files. Person.h Person.cpp This
I have two questions.They both are concerning a void in C++,which I am trying
I have two questions: 1) I have a few global variables for my website
I have two questions: (1) I learned somewhere that -O3 is not recommended with
I have two questions: From personal experience, what free blog engine is the best

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.