简体   繁体   English

如何在不同类中的类中操纵数组?

[英]How to manipulate array in a class, from a different class?

I hava a class, with an array in it. 我有一个类,其中有一个数组。 This is the code of the class currently: 这是当前类的代码:

public class SomeClass{
     int array[] = new int[3];
     array[0] = 1;
     array[1] = 1;
     array[2] = 1;
}

I want to create a class, that will contain a method. 我想创建一个包含一个方法的类。 That method sets all the numbers in the array inside SomeClass , equal to 0. 该方法将SomeClass数组中的所有数字设置为0。

If it was a method inside SomeClass , it would be very easy. 如果这是SomeClass中的方法,那将非常容易。 I would just add the follwing method to the existing class: 我只是将follwing方法添加到现有类中:

public void manipulateArray(){
     for(int i=0;i<3;i++){
          array[i]=0;
     }
}

But, I want this method to be inside a different class, and manipulate the array inside the class SomeClass . 但是,我希望此方法位于其他类中,并在SomeClass类中操纵数组。

What would be the most standard way to do this, inside an application? 在应用程序内部执行此操作的最标准方法是什么? Thanks 谢谢

You have to set this array field as a public static to access it from outside of the class and modify it: 您必须将此array字段设置为公共静态,才能从类外部访问它并进行修改:

public class SomeClass {
    public static int array[] = new int[3];
}

And then: 接着:

public void manipulateArray(){
     for(int i=0;i<3;i++){
          SomeClass.array[i]=0;
     }
}

Edit: 编辑:

However this is just quick solution. 但是,这只是快速解决方案。 Better is to create setter/getter for this field and work with objects of this class.. But.. Everything depends on your design. 更好的方法是为该字段创建setter / getter并使用此类的对象。..但是,一切都取决于您的设计。

  1. make your array public and access it via SomeClass sc = new SomeClass(); sc.array[0] = 4; 将您的数组公开,并通过SomeClass sc = new SomeClass(); sc.array[0] = 4; SomeClass sc = new SomeClass(); sc.array[0] = 4; Do not do that if you do not have to ! 如果没有必要,不要这样做!

  2. make your array static SomeClass.array[0] = 4; 使您的数组静态SomeClass.array[0] = 4; Do not do that if you do not have to ! 如果没有必要,不要这样做!

3.leave it private and make public setters getters like 3.将其设为私有,并让公共设置者像

public void setArray(int[] array) {
this.array = array;
}

SomeClass sc = new SomeClass();
sc.setArray(myNewArray);

Actually do that ! 居然做到了!

Why don't you simply implement getter for the array? 为什么不简单地为数组实现getter

In the other class you can have an object of type SomeClass and get the array from there: 在另一个类中,您可以具有SomeClass类型的对象,并从那里获取数组:

SomeClass someClass = new SomeClass();
int myArr = someClass.getArray();
someClass.modifyArray();

do like this. 做到这一点。

Pass the array to OtherClass.manipulateArray(array) and modify it. 将数组传递给OtherClass.manipulateArray(array)并对其进行修改。

class OtherClass{

    public void manipulateArray(int[] array){
       for(int i=0;i<3;i++){
            array[i]=0;
       }
    }
}

You can declare it like this and pass the array as an argument. 您可以像这样声明它,并将数组作为参数传递。

public void manipulateArray(int[] array){
     for(int i=0;i<3;i++){
          array[i]=0;
     }
}

I might be getting this wrong, as this answer is missing, but a standard way is to encapsulate the fields and manage object's state via the interface. 我可能会错了,因为缺少此答案,但是一种标准的方法是封装字段并通过接口管理对象的状态。 So in your case you could provide a reference to SomeClass and reset the array by a reference: 因此,在您的情况下,您可以提供对SomeClass的引用,并通过引用重设数组:

public class SomeClass{
     final int array[] = new int[3];

     { Arrays.fill(array, 1); }

     public void manipulateArray(){
         Arrays.fill(array, 0);  
     }     
}

public class OtherClass{
     SomeClass someClass;

     public OtherClass(SomeClass someClass){ this.someClass = someClass;}

     public void reset(){
         someClass.manipulateArray();
     }
}

above answers are true and useful. 以上答案是正确和有用的。 maybe depending to other parts of your code, declaring the array as "static" be useful, too. 可能取决于代码的其他部分,将数组声明为“静态”也是有用的。

You can use Java reflection to access the array of SomeClass from a different class. 您可以使用Java反射从其他类访问SomeClass的数组。

Here is small code for demonstrating it: 这是演示它的小代码:

public class POC {
    public static void main(String args[]) {
        new POC().run();
    }

    private void run() {
        SomeClass sc = new SomeClass();
        say("BEFORE: ");
        sc.showArray();

        DifferentClass dc = new DifferentClass();
        dc.manipulateArray(sc);

        say("---");
        say("AFTER: ");
        sc.showArray();
    }

    private class SomeClass {
        private int array[];

        public SomeClass() {
            array = new int[3];

            array[0] = 1;
            array[1] = 1;
            array[2] = 1;
        }

        public void showArray() {
            for (int i = 0; i < array.length; i++) {
                say(i + ">" + array[i]);
            }
        }
    }

    private class DifferentClass {
        public void manipulateArray(SomeClass sc) {
            try {
                Field arrayField = sc.getClass().getDeclaredField("array");
                arrayField.setAccessible(true);

                Object array = arrayField.get(sc);
                for (int i = 0; i < Array.getLength(array); i++) {// equivalent to for(int i=0;i<3;i++){
                    Array.set(array, i, 0); // equivalent to array[i]=0;
                }

                arrayField.setAccessible(false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private static void say(String msg) {
        System.out.println(msg);
    }
}

OUTPUT OUTPUT

BEFORE: 
0>1
1>1
2>1
---
AFTER: 
0>0
1>0
2>0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM