如何复制的HashSet和HashMap和它在Java使用指针? [英] How to copy hashset and hashmap, and does the Java use pointers?

查看:240
本文介绍了如何复制的HashSet和HashMap和它在Java使用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题:结果
第一:结果
我有它返回一个HashMap的功能。要读取返回的值,我把它写这样的:

 的HashMap<整数,字符串> HS = my_func2();

我做同样的如果函数返回HashSet的。

 的HashSet<整数GT; HS = my_func,并将();

我想知道,如果以这种方式返回的值复制到HS,或者我应该写一个深拷贝它或者我应该写这样的:
        HashSet的HS =新的HashSet(my_func,并将());
        HashMap的HM =新的HashMap(my_func2());

二quesion:结果
我通过调用make_matrix_funciton进行矩阵。矩阵woule是含有2维数组:搜索
[0 1 1结果
 0 0 0结果
 0 0 0]结果
然后我把这个矩阵sort_vec,并在此函数矩阵变化的因素。我认为Java不是基于指针,所以当我走出来sort_vec的,矩阵应该是因为它已经。但是,它已经改变了!这是结果
[0 0 0结果
 0 0 0结果
 1 1 0]结果
这表明,已被应用到它的sort_vec函数内的变化。这是正常的,如果是,我应该怎么做,以prevent它。在code以下是编译的。

 公共静态无效的主要(字符串ARGS []){
        INT矩阵[] [] =新INT [3] [3];
        矩阵= make_matrix(0.11亿);
        INT指数[] = {2,1,0};
        INT [] []垫= sort_vec(3,矩阵索引);
    }    私有静态诠释[] [] sort_vec(INT motifsize,INT [] []垫,INT []索引)
    {
        INT [] = main_index {0,1,2};        INT L = indexes.length;
        对于(整数i = 0; I<升;我++)
            如果(索引[I]!= main_index [I])
            {
                INT J =索引[I]
                INT K = main_index [I + 1];
                对于(; K< l; k ++)
                    如果(指数[K] == main_index [I])
                        打破;                索引[K] = j的;
                垫=交换(motifsize,垫,J,main_index [I]);
            }
        返回垫;
    }
    私有静态诠释[] []交易所(INT motifsize,INT [] []矩阵,诠释的x,int y)对
    {
        INT温度;        的for(int i = 0; I< motifsize;我++)
        {
            TEMP =矩阵[I] [X]
            矩阵[I] [X] =矩阵[I] [Y];
            矩阵[I] [Y] =温度;
        }
        的for(int i = 0; I< motifsize;我++)
        {
            临时=矩阵[X] [I];
            矩阵[X] [I] =矩阵[Y] [I];
            矩阵[Y] [I] =温度;
        }        返回矩阵;
    }
    私有静态诠释[] [] make_matrix(字符串ID)
    {
        INT矩阵[] [] =新INT [3] [3];
        INT C = 0;
        为(中间体X = 0; X 3;; X ++)
            为(中间体Y = 0; Y 3;; Y +)
            {
                如果(id.charAt(三)=='1'|| id.charAt(三)=='5')
                    矩阵[X] [Y] = 1;
                C ++;
            }
        返回矩阵;
    }


解决方案

Java的总是按引用传递对象,所以如果你从一个函数返回一个HashMap对象,引用将被传递到HS变量在你的榜样。路过HashSet的一个新的HashSet实例的构造函数将无法工作。这将创建一个相同的对象引用新的HashSet作为原之一。如果修改这些对象之一,改变将出现在所有其他的参考点了。

如果你想完全分离的副本,你将需要自己的方法的深拷贝,因为在的JavaDoc为它的clone()方法说:


  

返回此HashSet的实例的浅表副本:本身没有复制的元素


这同样适用于数组。每个数组是一个对象,因此,如果您修改元素,它会修改该数组的所有引用。要创建一个延迟拷贝,使用<一个href=\"http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/System.html#arraycopy%28java.lang.Object,%20int,%20java.lang.Object,%20int,%20int%29\">System.arrayCopy

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;
    }

解决方案

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

这篇关于如何复制的HashSet和HashMap和它在Java使用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆