小编Zac*_*lor的帖子

创建了类的新实例或仅分配了内存空间?

UPDATE

public Fish mate(Fish other){
  if (this.health > 0 && other.health > 0 && this.closeEnough(other)){

    int babySize = (((this.size + other.size) /2));
    int babyHealth = (((this.health + other.health) /2));
    double babyX = (((this.x + other.x) /2.0));
    double babyY = (((this.y + other.y) /2.0));

    new Fish (babySize, babyHealth, babyX, babyY);
  }
  return null;
}
Run Code Online (Sandbox Code Playgroud)

什么时候new Fish被调用,是否有一个新的实例Fish在没有引用的地方浮动或者我刚刚为新Fish实例分配内存而没有实际实例化它?

我可以new Fish调用创建一个Fish具有唯一引用名称的实际实例,而不是迭代循环吗?

java instance

6
推荐指数
1
解决办法
192
查看次数

C - 结构和函数的前向声明

我试图弄清楚前向声明究竟是如何相互作用的。当前向声明一个采用 typedef 结构的函数时,有没有办法让编译器接受先前前向声明(但实际上未定义)的结构作为参数?

我已经开始工作的代码:

typedef struct{
  int year;
  char make[STR_SIZE];
  char model[STR_SIZE];
  char color[STR_SIZE];
  float engineSize;
}automobileType;

void printCarDeets(automobileType *);
Run Code Online (Sandbox Code Playgroud)

我希望我能做什么:

struct automobileType;
void printCarDeets(automobileType *);

//Defining both the struct (with typedef) and the function later
Run Code Online (Sandbox Code Playgroud)

我觉得我要么错过了一些非常基本的东西,要么不了解编译器如何处理结构的前向声明。

c struct typedef forward-declaration

5
推荐指数
1
解决办法
5309
查看次数

仅使用数组生成不重复的随机数

我想X用从0到的随机整数填充一个大小的数组,X没有重复。问题是我必须只使用数组来存储 的集合int,而不是ArrayLists。我该如何实施?

我不明白为什么我似乎无法得到这个。但这是我最近的一段代码,它填充了列表但允许重复。

System.out.print("Zero up to but excluding ");
int limit = scanner.nextInt();

// create index the size of the limit
int [] index = new int[limit];

for(int fill=0;fill<limit;fill+=1){
    index[fill] = (limit);
}

int randomNumber = 0;
Random rand = new Random();
int [] randoms = new int[limit];
boolean flag = true;

// CODE TO NOT PRINT DOUBLES
for (int z=0;z<limit;z+=1){
    randomNumber = rand.nextInt(limit);
    int i=0;
    while (i<limit){
        if (index[i] …
Run Code Online (Sandbox Code Playgroud)

java

2
推荐指数
1
解决办法
2995
查看次数

标签 统计

java ×2

c ×1

forward-declaration ×1

instance ×1

struct ×1

typedef ×1