0

I have a problem to convert from int to int []. I have tried to modify the coding but still have error. I want to change the method getRandomNumberInRange into int[] because i need to combine with [hostType] and [hostType] is in array form.

// this method is to convert from int to int[]
static Integer[] toObject(int[] intArray) {

  Integer[] result = new Integer[intArray.length];
  for (int i = 0; i < intArray.length; i++) {
   result[i] = Integer.valueOf(intArray[i]);
  }
  return result;
 }

// this method to generate random number
public static int getRandomNumberInRange(int min, int max) {

  if (min >= max) {
   throw new IllegalArgumentException("max must be greater than min");
  }

  Random r = new Random();
  return r.nextInt((max - min) + 1) + min;
 }

//this method is to implement the function getRandomNumberInRange and need to be in array form
public static List<PowerHost> createHostList(int hostsNumber) {
  List<PowerHost> hostList = new ArrayList<PowerHost>();
  for (int i = 0; i < hostsNumber; i++) {
   int hostType = i % Constants.HOST_TYPES;

//   int mips2[]=(int) getRandomNumberInRange(100, 1000);
   List<Pe> peList = new ArrayList<Pe>();
   for (int j = 0; j < Constants.HOST_PES[hostType]; j++) {
    int[] obj = new int[hostType] ;
    Integer[] newObj = toObject(obj);
    peList.add(new Pe(j, new PeProvisionerSimple(getRandomNumberInRange(100, 1000)[newObj])));
   }
UNKNOWN
  • 13
  • 7

1 Answers1

0

There are a few things wrong. First, in the last code snippet, you are missing two '}'s. Second, getRandomNumberInRange(int min, int max) returns an int, which is not an array. What that means is that you wouldn't do getRandomNumberInRange(100, 1000)[newObj] because that is like doing 107[4]. 107 isn't an array so that wouldn't work. Also, newObj is an array, so even if getRandomNumberInRange returned an array, newObj wouldn't be able to be used as an index to get the int in the array. This is because the index (the thing that goes in array[here]) must be an int.