0
I have the following error in compiler

return  ans;
                ^
  required: byte[]
  found:    int[]

I have this method in which i want to return a type of byte . Can anyone tell me how can i cast ans to make it compatible with me method type

public  byte[]  element(int m) {
    //if (q!= a.length)
    // throw new Exception("Array length does not equal k");
    int f;
    int q;
    int[] t;

    this.first = f;
    this.second = q;
    this.data = new int[q];

    for (int i = 0; i < t.length; ++i) {
        this.data[i] = t[i];
    }

    // if (!this.IsValid())
    //  throw new Exception("Bad value from array");


    int ans[] = new int[this.second];

    int a = this.first;
    int b = this.second;
    int x = (choose(this.first,this.second) - 1) - m; // x is the "dual" of m

    for (int i = 0; i < this.second; ++i) {
        ans[i] = largestV(a, b, x); // largest value v, where v < a and vCb < x
        x = x - choose(ans[i], b);
        a = ans[i];
        b = b - 1;
    }

    for (int i = 0; i < this.second; ++i) { 
        ans[i] = (first-1) - ans[i];
        return  ans;
    }
}

Note: i want it to return type of byte not int but i am trying to cast ans but without success .

Harmelodic
  • 5,682
  • 1
  • 28
  • 31
john smith
  • 21
  • 6
  • Possible duplicate of [How to convert int\[\] to byte\[\]](http://stackoverflow.com/questions/1086054/how-to-convert-int-to-byte) – Harmelodic Mar 10 '16 at 17:33
  • 1
    Why create it as an int[] if you want a byte[]? Just create it as a byte and do your casting in the assignment "ans[i] = (byte) largestV(...)". – BarrySW19 Mar 10 '16 at 17:35

2 Answers2

0

You have to change int[] ans to Byte[] ans, you are trying to return an int array, but you have to return your byte array

int f;
   int q;
  int [] t;

    this.first = f;
    this.second = q;
    this.data = new int[q];
    for (int i = 0; i < t.length; ++i)
      this.data[i] = t[i];

   // if (!this.IsValid())
    //  throw new Exception("Bad value from array");


        byte ans []= new byte[this.second];

        int a = this.first;
        int b = this.second;
        int x = (choose(this.first,this.second) - 1) - m; // x is the "dual" of m

        for (int i = 0; i < this.second; ++i) {
            ans[i]= largestV(a, b, x); // largest value v, where v < a and vCb < x
            x = x - choose(ans[i], b);
            a = ans[i];
            b = b - 1;
        }

        for (int i = 0; i < this.second; ++i)

            ans[i] = (first-1) - ans[i];

        return  ans;
    }
user3272931
  • 45
  • 10
  • i tried that before i posted my question and is not working because all the code below byte ans []= new byte[this.second]; must be casted into byte then – john smith Mar 10 '16 at 17:54
  • @johnsmit then that's what you'll have to do. You can cast `int` to `byte` but not `int[]` to `byte[]`. – Louis Wasserman Mar 10 '16 at 17:56
0

Dunno why you would wanna to work on int and return a byte. Dat aside, You can create a new byte array of the same size as ans and do this in the last loop

for (int i = 0; i < this.second; ++i)
{ 
    ans[i] = (first-1) - ans[i]);
    ans_byte_version[i] =  (byte) ((first-1) - ans[i]);
}
return ans_byte_version;

However it'd be better if you tell us what are you trying to do. Why have you declared int when you want byte in the first place.

gallickgunner
  • 472
  • 5
  • 20