【问题标题】:Assign values to Array of Objects [closed]为对象数组赋值[关闭]
【发布时间】:2018-01-17 22:02:46
【问题描述】:

我正在尝试创建一个对象数组。我为 10 个节点分配了一个“node_id”。我还在为类中的其他变量赋值。 'networkInitialization()' 方法中的 for 循环以顺序方式将值分配给 'node_id',但是当我尝试在 main 方法中打印它时,它返回所有 '9'。

import java.util.*;
public class tman {
  static int N = 10;
  static int k = 5;
  static Node nodes[] = new Node[N]; 
  public static void main(String[] args) {
    networkInitialization();
    for(int i = 0; i<N; i++){
      System.out.println(nodes[i].node_id);
    }
  }
  public static void networkInitialization(){
    Random random = new Random();
    int next;
    double theta;
    System.out.println("Initializing the network");
    for(int i = 0; i<nodes.length; i++){
      nodes[i] = new Node();
      HashSet<Integer> used = new HashSet<Integer>();
  //Nodeid
      nodes[i].node_id = i;
  //        System.out.println(nodes[i].node_id);
  //Generating 'k' random neighbors list
      for(int j = 0; j<k; j++){
        next = random.nextInt(10);
        while (used.contains(next)) { //while we have already used the number
            next = random.nextInt(N); //generate a new one because it's already used
        }
        used.add(next);
//          System.out.println(next);
        nodes[i].neighbors[j] = next;
      }
//Calculating XCo and YCo
      theta = 3.14/2-(i-1)*3.14/(N-2);
      nodes[i].x_co = Math.cos(theta);
      nodes[i].y_co = Math.sin(theta);
      nodes[i].theta = theta;
//      System.out.println(nodes[0].x_co);
    }
  }
}
class Node{
  static int node_id;
  static double x_co;
  static double y_co;
  static double theta;
  static int k = 30;
  static int neighbors[] = new int[k];
  static Map<Integer, int[]> received_list = new HashMap<Integer, int[]>();
  int N;

  public static int getNodeId(){
    return node_id;
  }

  public static double getXCo(){
    return x_co;
  }

  public static double getYCo(){
    return y_co;
  }

  public static double getTheta(){
    return theta;
  }

  public static int[] getNeighbors(){
    return neighbors;
  }

  public static Map<Integer, int[]> getReceivedList(){
    return received_list;
  }

  public void setNodeId(int node_id){
    this.node_id = node_id;
  }

  public void setXCo(int x_co){
    this.x_co = x_co;
  }

  public void setYCo(int y_co){
    this.y_co = y_co;
  }

  public void setTheta(double theta){
    this.theta = theta;
  }

  public void setNeighbors(int neighbors[]){
    this.neighbors = neighbors;
  }
}

这是我在 main 方法中得到的输出。

Initializing the network
9
9
9
9
9
9
9
9
9
9

任何人都可以帮助我吗?

更新: 看起来我对static 的理解还不够好。删除 networkInitialization() 方法中的所有静态工作正常。谢谢。

【问题讨论】:

标签: java arrays class object arrayobject


【解决方案1】:

正如 cmets 中所指出的,问题在于 staticNode 的定义中的使用。如果一个变量是static,这意味着它对于该类的每个实例都是相同的。为了说明这一点,请看下面的代码:

Node a = new Node();
a.node_id = 1;

Node b = new Node();
b.node_id = 2;

如果node_id 不是静态的,它的行为将与您预期的一样:Java 创建一个Node a,其中有一个字段node_id 设置为1,然后继续创建另一个Node b,它具有它自己的字段node_id,自然不同于节点a的node_id

但是,如果node_id 是静态的,则所有节点共享同一个字段node_id!这意味着Node a 将共享node_id 设置为1,但Node b 访问相同的node_id 并将其设置为2。

在您的示例中,最后一次调用共享字段node_id 是在i = 9 时,因此共享的node_id 的值为9,无论它是哪个节点。

你说你是初学者,所以你可能不会经常使用static。但是,static 可能很有用,例如,如果您想知道总共有多少个节点。在这种情况下,您可以有一个 static 字段来跟踪节点的数量。每次调用另一个节点时,您都​​会增加该数量。因为它是共享的,所以所有节点都会知道总共有多少个节点。如果您想了解更多关于如何使用static,请参阅Java tutorial

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2019-03-14
    • 2020-10-05
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多