0

I'm trying to create an Integer[] that is increased by a multiple of 10 for each loop. Once the Integer[] size is set I'd like it to fill up the array with random ints. I'm able to get the size of the array to increase, but the values stored within it are null. To me this means the array is resizing correctly, but they elements aren't being assigned to anything. I'm trying a double for-loop, with the inner loop assigning the random values. If there is a better way to do this(which I'm sure there is b/c mine isn't running!) could you please help?

this is my creation of the Int[]

 public class TimeComplexity {

    Integer[] data;

    public TimeComplexity() 
    {
        Random random = new Random();

        for (int N = 1000; N <= 1000000;  N *= 10) 
        {
            N = random.nextInt(N);
            data = new Integer[N];
            //checking to see if the random numbers were added.
            //array size is okay but locations aren't taking a 
            //random number
            System.out.println(Arrays.toString(data));

        }

    }

In case you're interested in the output for it my main class. (this isn't part of the question but if you have suggestions I'd love them!)

public class TimeComplexityApp {

    private static int MAXSIZE = 1000000;
    private static int STARTSIZE = 1000;

    public TimeComplexityApp() 
    {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) {

        TimeComplexity time = new TimeComplexity();
        System.out.println(time);
        System.out.printf("%-6s %13s %13s\n\n\n","ARRAY","int","INTEGER");

        for (int N = STARTSIZE; N <= MAXSIZE;  N *= 10) 
        {
            double d = 1.0;
            System.out.printf("\n%-6d %15.2f %15.2f\n", N, d, d);
        }
    }

}
3
  • You don't actually have a double for loop there. You need an inner loop that will assign the random numbers to each index. Did you leave it out when copying your code, or is this your current code? Commented Apr 24, 2015 at 17:39
  • This is my current code Commented Apr 24, 2015 at 17:46
  • Then you need another loop that iterates through and sets values. You have extended the length, but you never assign them to anything. Looks like @ControlAltDel 's code snippet is a good example of a for that you can put inside your existing for loop. Commented Apr 24, 2015 at 17:49

2 Answers 2

1

In the first source code that shows lack initialize the elements of array of integers.

data = new Integer [N]; only creates the array of integers of size N, lack include the elements in each cell of the array.

So, just need a loop to complete each element or cell array:

for (int i = 0; i <N; i ++)
    data [i] = random.nextInt (N);

Now this array is complete and will not return NULL on each item.

Sign up to request clarification or add additional context in comments.

Comments

0

On every iteration of your loop, you are creating a new array of random (int size) length. But you are never putting anything into it. The right way to do this is:

int[] vals = ...;
for (int i = 0; i < end - start; i++) {
  if (vals.length < i; i++) {
     //1. create new larger int[]
     //2. copy the old array into the new array
     //3. vals = yourNewArray
  }
  vals[i] = random.nextInt();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.