0

Can you create a line of code, within a while-loop, that will create a new array AND change the array's name with each iteration of the while loop?

Example:

int size = 10;
int name_count = 1;

while(size <= 100)
{
    //name_count is changing the name of the array by calling it
    // "array1", "array2", etc...
    //I know this may not be correct code for changing the name of
    // the array, but it is suppose to get the point across.
    int[] array(name_count) = new int[size]; 

    for (int i = 0; i <= size; i++)
    { /* Adding numbers to an array */ }

    size = size + 5;
    name_count++;
}
3
  • 2
    Why would you want to change the array's name with every iteration of the loop? Not sure if get the logic behind that. :) Regardless, your code wouldn't actually be changing the name of the array--it would create a NEW array, not containing any of the old numbers. Commented Mar 26, 2012 at 16:35
  • I need to create 19 different arrays with the first array conatining 10 signed integers and each array's size will increase by 5 until 100 is reached. Hence, wanting to change the array name each iteration. Commented Mar 26, 2012 at 16:50
  • Are you trying to recreate a data structure, such as ArrayList, that grows its array size, underneath the hood, as more elements get added? You've got two options you can use that might solve your problem more efficiently. First, just create all your arrays in advance. Or secondly, every time you increment by 5, create a new array with size = oldsize + 5 and copy the old values into the new array. This way your array would grow dynamically like ArrayList. Commented Mar 26, 2012 at 17:19

2 Answers 2

2

Identifier names need to be defined at compile time. So you can't explicitly use a different variable name on each iteration of the loop.

Another problem with your pseudo-code is that, if the array were to be declared inside the loop, it would fall out of scope when the loop completes, so there wouldn't be much point.

To do something like this you need to use some collection to hold the arrays, and it would be easier to make them explicit objects instead of just arrays. Something like:

List<List<Integer>> listOfArrays = new ArrayList<List<Integer>>();
while (size <= 100) {
  List<Integer> listOfNumbers = new ArrayList<Integer>(size);
  /* insert loop here to add numbers to listOfNumber */

  size += 5;
  name_count += 1;
}

Then you can access each list of numbers using an index into listOfArrays -- equivalent to naming each one with the index, but handled at runtime instead of compile time.

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

2 Comments

I see where you are going with this and the dynamics is useful. The only issue I feel I will run into is that the integers that will be put in each element will be using 'Random.nextInt()'. I can go more into detail about the project, but I would rather not on here.
I don't see what issue you think that might cause.
0

You cannot change the array's name, It will just re-declare the array with each successful loop. (It will be a new blank array.) I think what you are looking for is a two dimensional array.

int[][] myArray = new int[3][3];

2 Comments

I don't want to use a 2D Array because I have to create 19 different arrays with Array1 being size 10 and each array increases in size by 5 until size 100 is reached. Due to this, there would be too much empty space in the array for what my project is asking me to do after this fact. This part is only 5% of my total project and this is the part I am having the most issues with.
I see your point, I believe Dave Costa's answer with the ArrayList might be the solution. Give it a try.

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.