-1

I have the following array:

public int[] ID = {0,1,2,3,4};

I´m calling the following method, which contains an get-method in an framework: int[] marker = this.m_TuioManager.getID(this.ID);

Get-Method in the framwork class:

 public int[] getID(int[] wert)
    {
        int number= 4;
        for (int i = 0; i<=number; i++)
        {
            for (int j = 0; j<=number; j++)
            {
                wert[i] = wert[j];

            }
        }
        return wert;
    }

The exception is:

Array index out of range

which I don´t understand. Cauze my array has 5 numbers from 0 to 4. And in the for loop I´m iterating until 4. What I want later, is to compare the values in the array like wert[0] = 0, wert[1] = 1 and so on(until 4),with an other variable.

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
Bibi90
  • 1
  • 2
  • 2
    I don't see anything that would throw that here. Have you tried to step through it to see exactly where this is happening? – Stefan H Dec 28 '17 at 23:18
  • 2
    Your [code as shown works](https://ideone.com/vEfTQF), assuming your array `ID` goes into `getID`. Create an [MCVE] that demonstrates the problem you claim to have. – rene Dec 28 '17 at 23:19
  • the nested loop will write only the last value of `wert` into each position that is indexed by `i`. so you code will result in an array filled with `4`'s : `{4,4,4,4,4}`. also, since the `int[] wert` is a reference type you are actually manipulating the input values and there is no need to return `wert` because you overwrite the original object – Mong Zhu Dec 28 '17 at 23:30
  • might it be that this exception is thrown when you try to compare 2 arrays?=! and the other array has less or more elements than `marker` ? – Mong Zhu Dec 28 '17 at 23:32
  • Thanks. The exception line is on the wert[i] = wert[j]. What it also says after the exception is: getID(System.Int32[wert]). And sorry, I also have forget to say, that I´m using unity3D. I have go with steps there, just with showing Debug.Logs. – Bibi90 Dec 28 '17 at 23:36
  • While you are editing above information into the post and in addition to fixing your ... misinforming everyone on how you call your `getID` method, could you please explain what you expect this `getID` method actually do? (as it seem to just using constant `{4,4,4,4,4}` would do the same...) – Alexei Levenkov Dec 28 '17 at 23:38
  • @Bibi90 From this code only there's no exception. You have to provide more information of this example – Greggz Dec 28 '17 at 23:40
  • if the exception is really on that line, than you seem not to pass the array that you have posted into the call of your method. – Mong Zhu Dec 28 '17 at 23:40
  • Maybe you know it is 0-4 but you do not test and that is 5 – paparazzo Dec 28 '17 at 23:42
  • Thanks for your answers. The method getID(this.ID) is needed to compare later the values in the array with an other integer. So I want later to compare for example marker[0] with 0, marker[1] with 1 and so on. And yes I´m really passing the array ID, to the method getID() with jus 5 numbers from 0-4. And I got it, that I have made a mistake, that it only returns {4,4,4,4} as you said. But it shouldn´t throw this exception, am I right? – Bibi90 Dec 28 '17 at 23:53
  • I suggest you change int number = 4 to int number = wert.length or at least have an if check to confirm the length of wert. Otherwise, if the input is correct, it shouldn't throw an error. – SILENT Dec 28 '17 at 23:56
  • no it should not throw any exceptions. The problem here is that we cannot reproduce your situation with the information that you gave us. sorry – Mong Zhu Dec 29 '17 at 00:40

1 Answers1

0

If you want to avoid "Array index out of bounds" errors, don't write code that assumes arrays always have X number of elements. Either check the .Length of the array before you attempt to access 4 elements of it, or write a loop that runs to the array.Length rather than always running to a fixed 4 elements

if(array.Length < 4)
  throw new Exception("min array length is 4, you passed " + array.Length);

Or

for(int i = 0; i < array.Length; i++)

Or

foreach(int i in array)

If you can get any of these lines of code to crash with an AIOOB exception, inform Microsoft

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • I´ve already tried it with foreach and with wert.Length Thanks. But with the if statement I see the problem: wert.Length is 0, so the array isn´t passed as I´ve thought. But I´still don´t know, why I´ts not passing my array ID right. Do I have to initialize it with int ID[] = new int[5]? I guess that doesn´t solve it.. – Bibi90 Dec 29 '17 at 00:12
  • You'll need to post the full code, but one thing is for sure, that array of 5 ints you thought you were passing, ain't the one that is being passed! You don't need to make a new int[5] if you're i Italian the array with 5 bpvalues. The compiler will expand that code for you, into an array of size 5 and set each index to the given value in the initializer – Caius Jard Dec 29 '17 at 00:19