Loading...
Logo
Processing Forum

Index 1, Size 1?

in Programming Questions  •  2 years ago  
Copy code
  1. List<Item> PItems;
  2.   Item[] HItems = new Item[10];

  3. int HCount = 0;
  4.     for (HCount = 0; HCount < HItems.length; HCount++) {
  5.       HItems[HCount] = PItems.get(HCount);
  6.     }

What I wanted to do was to get the first 10 items from PItems and transfer them inside HItems

But the thing is that I keep getting IndexOutOfBoundsException: Index: 1, Size: 1 error

The loop is inside the draw function*

Replies(6)

Re: Index 1, Size 1?

2 years ago
Are you sure there are 10 items in PItems as the code is Ok otherwise?

Re: Index 1, Size 1?

2 years ago
I am sure, I don't see why would it do that...

Re: Index 1, Size 1?

2 years ago
we can't tell from the tiny bit of code you posted. but that error is bascially saying that you have nothing in PItems. check your initialisation. certainly nothing in the code you posted is setting PItems.

try printing out the length of PItems before the loop.

btw, the convention in java circles is that variables start with lower case letters, classes with uppercase.
Hmm.. Does it really matter? Like I mean is there a significant difference?

Re: Index 1, Size 1?

2 years ago

Hmm.. Does it really matter? Like I mean is there a significant difference?


If you were writing a book you wouldn't ask this question because the answer is obviously yes.

In programming the answer is not so obvious because the Java compiler does not care about conventions. But programmers do because makes it easier for them to read, understand, debug, extend, use each others code.

In your code above you have indented your code - that is a programming convention, why did you do that since the compiler doesn't give a hoot. The reason is that It makes the stucture of your program clearer.

On a personal level if you learn to use these conventions it will make it easier for others to help you with your code.

You might like to following the links from this webpage from Oracle

Re: Index 1, Size 1?

2 years ago
well yes, it's what people are used to.

pItems.get(5);

PItems.get(5);

make me think of two entirely different things. the first is accessing the 5th element of a list called pItems. the seconds is calling a static method called get on the PItems class with the value 5.

make it hard(er) for me to read and understand your code and there'll be a point where i won't bother.

probably ok when you're working alone but when you're working as part of a team (or when asking for help) and it's a good idea to stick to the conventions, makes it easier for everybody.

(ha ha quarks)