无法在数组类型int []上调用我的方法 [英] Cannot invoke my method on the array type int[]

查看:140
本文介绍了无法在数组类型int []上调用我的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习如何使用遗传算法.但是,我在使用方法compareTo()时遇到了困难.此方法应比较两个人之间的适应度值.我一直在尝试调用我的getFitness()方法以使其在compareTo()方法中使用,但我一直收到此错误

I'm currently learning how to use genetic algorithms. However, I'm having difficulty with my method compareTo(). This method should compare the fitness values between two individuals. I've been trying to invoke my getFitness() method for it to be used in the compareTo() method, but I keep getting this error

无法在数组类型int []上调用getFitness()

Cannot invoke getFitness() on the array type int[]

我不确定此错误来自何处.附带的将是提到的方法和构造函数,我将概述发生此错误的位置.谢谢.

I'm not sure where this error is coming from. Attached will be the mentioned methods and constructors, I will outline where this error occurs. Thank you.

public class Individual implements Comparable<Individual> {
  Random rand=new Random(); //Initialize random object
  private int size; //Represents N queens
  private int[] individual; //Represents the array of possible solutions

  public Individual(int[] permutation) {
    individual=permutation;
    size=individual.length;
  }

  public Individual(int size) {
    this.size=size;
    individual=Util.getPermutation(size); //Constructs an individual object that is an array of ints
  }

  public int getFitness() {
    //How many queens are attacking each other on the diagonal
    // 0 Represents the best fitness, this is the solution

    //Number of possible differences in permutation is found through factorial addition
    int newSize=0;
    for (int i=0; i<=size; i++){
      newSize+=i;
    }
    int fitness=0;
    int [] xDifference=new int[newSize]; //Array that stores distance between Columns
    int [] yDifference=new int[newSize]; //Array that stores distance between rows

    //Calculates the distance between columns and stores them
    for (int i=0; i<size; i++){
      for (int j=i+1; j<size; j++)
        xDifference[i]=j-i;
    }

    //Calculates distance between rows and stores them
    for (int i=0;i<size;i++){
      for (int j=i+1; j<size; j++)
          yDifference[i]=Math.abs(individual[j]-individual[i]);
    }

    //Compares xDifference and yDifference elements, if they are equal that means Queens are attacking
    for (int i=0; i<size; i++){
      if (xDifference[i]==yDifference[i]){
        fitness++;
      }
    }

    return fitness;
  }

  public int compareTo(Individual other) {
    int compare;

    if (individual.getFitness()<other.getFitness()){ //ERROR OCCURS HERE
      compare=-1;
    }else if (individual.getFitness()>other.getFitness()){// ERROR HERE
      compare=1;
    }else {
      compare=0;
    }

    return compare;
  }
}

推荐答案

问题是您正在调用individual.getFitness(). individual确实是一个int[],而不是一个Individual对象.相反,您将要使用

The problem is that you are calling individual.getFitness(). individual is indeed an int[], not an Individual object. Instead, you'll want to use

if (this.getFitness()<other.getFitness()){ 
     compare=-1;
}

您也可以省略this.,但是我个人发现它使代码更易于阅读.

You could also leave out this., but I personally find that it makes code easier to read.

这篇关于无法在数组类型int []上调用我的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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