Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Naming Arrays  RSS feed

 
Chris Lenney
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array called chromosomes of 20 Strings:
String chromosomes = [chromosome0,chromosome1,chromosome3... chromosome19]
I wish to loop through this array creating integer arrays of each String. The following code isn't valid due to incompatible types:

for (int i = 0; i < 20; i ++) {
String current = chromosomes[0];
current = new int[500];
}

How can i name the desired integer arrays with each of the 20 Strings in the chromosomes array? I cannot hard code the names in because in practise there will be an unknown number of them!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't assign a String to an int array. You can either use the split method or charAt to extract the elements from the String.
 
Chris Lenney
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply, I shouldn't of written so much, what i really want to know is how arrays can be created in a loop? Can I create a loop which takes in value (say 20) and then creates 20 arrays?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would do something like this.

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok...so you want to load the String array with strings right?


int count = 0;
for ( int i = 0; i<array.lengh; i++)
{
count++;
array[i] = ("chromosome" + count);

}

for(int i = 0; i<array.length;i++)
{
System.out.println(array[i]);
}

oh! i see what you want to do

you want to have a 2-D array

what you do is

string [][] = new String [20][chromosome characteristics];

and then load the variables like i did,

except you would have another for loop

for (int j = 0;j<array[i].length; j++)

{
//then you would load the character0 characteristics

array[i][j] = first characteristic;
// then when the loop happens again
// it will store the second characteristic of chromosome0


and when the array[j] = array[i].length

it will loop to the chromosome1

well i hope this helps

Monk..
 
Chris Lenney
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for all your comments, very helpful, looks like its 2D arrays. Can you just answer one last thing, I have the following problem:

int[] array1 = new int[20];
String num = "1";
String str = "array" + num; //making str = array1

How can i reference array1 using the String 'str'
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would recommend using a HashMap.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!