0
import java.util.*;


public class BinaryTest  {

    public static void main(String[] args) {



        //declare array here


        int size = 1; 
        for(int i = 0; i<=size; i++){ 
            Integer[] iarray = new Integer[i]; 
            if(size <= 64){ 
                for(int j = 0; j<=(size-1)*2; j++){ 
                    iarray[j] = j*2;
                    size++; 
                }
            }
            System.out.println("Element at index " + i + ": " + iarray[i]);
        }


    }
}

I wanna print the array from sizes from 0 to 64 and print it with number in it. It gives me an: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at BinaryTest.main(BinaryTest.java:19) So is there a way I can solve this?

  • You probably want `for (int i = 1; ...` instead of `for (int i = 0; ...` – Dawood ibn Kareem Jan 18 '17 at 03:39
  • If you allocate an array of size 0, it has no elements, so you will get ArrayIndexOutOfBoundsException any time you try to access any element. However, there are so many things wrong with your code that I'd suggest finding a tutor or knowledgeable friend to help walk you through this. It will be hard to help you with your problem except just to give you the code, which isn't going to benefit you at all. – ajb Jan 18 '17 at 03:45

0 Answers0