Java,类和对象中的数组 [英] Java, arrays in classes and objects

查看:120
本文介绍了Java,类和对象中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Java创建某种数组实用程序类.一切都很好,但是在对数组进行排序时遇到一个问题.

这是我的数组类:

I''m trying to make some kind of array utility class in Java. Everything is ok, but i''m having one problem when sorting arrays.

Here is my array class:

public class ArrayTools
{
    private int [] array1    = new int[5];
    private int [] array_out  = new int[5];
    private int temp;
    public void setArray (int [] in)
    {
        array1 = in;
    }
    public int[] getArray()
    {
        return array1;
    }
    public int[] getArraySort()
    {
        return array_out;
    }
    public void sortArray()
    {
        array_out = array1;
        for (int i=0; i<5; i++)
        {
            for (int j=0; j<4; j++)
            {
                if ( array1[j+1] < array_out[j])
                {
                    temp = array_out[j];
                    array_out[j] = array_out [j+1];
                    array_out[j+1] = temp;
                }
            }
        }
    }



在setAaarray中,我设置了从主程序获取的数组. getArray应该返回原始数组,getArraySort应该返回排序后的数组. sortArray显然是排序功能.

主程序很简单:



In setAaarray, i set up array which i get from main program. getArray should return original array, getArraySort should return sorted array. sortArray is obviously sorting function.

Main program is simple:

ArrayTools na = new ArrayTools();
int [] testa = new int[5];
na.setArray(testa);
na.sortArray();



问题是,当我调用 sortArray 函数时,它还会对类中的 array1 数组进行排序,而不仅仅是 array_out 数组.

我想我错过了一些非常简单的东西...



Problem is that when i call sortArray function it also sorts array1 array in class, not just array_out array.

I guess i''m missing something very simple...

推荐答案

您很幸运,这很简单:).在sortArray()的第一行中设置array_out时,只需将对array1的引用复制到array_out中.然后,两个成员都引用内存中的一个数组.
为了创建数组的第二个副本,可以使用System.arraycopy:
删除以下行:
Your''re lucky, it is simple :) . When you set array_out in the first line in sortArray(), you just copy the reference to array1 into array_out. Both members reference then to one array in memory.
In order create a second copy of the array, you can use System.arraycopy:
Remove the following line:
array_out = array1



并替换为:



and replace it by this:

//Creates an array with the same size as array1
array_out = new int[array1.length];
//Copies the content of array1 (Parameter 1) beginning at index 0 (Parameter 2) 
//into array_out (Parameter 3 beginning at index 0 (Parameter 4). Parameter 5
//is the number of elements to copy
System.arraycopy( array1, 0, array_out, 0, array1.length );



Stefan



Stefan


我说这很简单:
I''d say it was pretty simple:
array_out = array1;

也许,如果您在排序之前创建了一个新数组并将"array1"复制到该数组中,那么一切都会如您所愿!目前,您正在使用两个引用相同的数据...

请参见 Java:如何复制数组 [

Perhaps if you created a new array and copied "array1" into it before you sorted it, things would work out how you wanted! At present, you are working with two references to the same data...

See Java: How to copy an array[^]


谢谢大家,我知道那是第一行有东西...
Thanks guys, i knew that was something with the first line...


这篇关于Java,类和对象中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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