0

I have a String array myArray which is filled by picking data from mysql database. The database is dynamic and keeps changing and as the database changes, the elements in myArray also keep on changing. I have already achieved the above in my code.

I now want to create an array of integers but with the array name being the elements in myArray. For example, if I have:

String[] myArray = new String[2];
myArray[1] = "string1";
myArray[2] = "string2";

The above "string1" and "string2" are coming in from the database. Once myArray is populated, I now want to create an array of integers having names string1 and string2 like below:

int[] string1 = new int[10];
int[] string2 = new int[10];

Currently, myArray size is 30 but might go up to 50.

I am a java newbie and would appreciate if any java folks could tell me how I could achieve the above in my code.

Many thanks

Gentleman
  • 119
  • 2
  • 8
  • So what you want is assigning the variable's name to be the same of first array element? If is so, nope, you cant. I suggest you using a map – Leviand Jul 11 '18 at 07:18
  • Use a 2D array: `int[][] strings` – ernest_k Jul 11 '18 at 07:18
  • 1
    That's not possible, and for good reason. Variable names are just for you, they don't matter to the compiler. Therefore even if you were able to create an array name dynamically, it would be useless. Now you definitely need to read a proper tutorial, but first you should edit your question to describe what you want to do, not how you think you can do it. – Kayaman Jul 11 '18 at 07:19
  • 1
    I'm guessing you want something like a `HashMap`? – UnholySheep Jul 11 '18 at 07:19

2 Answers2

2

Try HasMap:

HashMap<String,String> hm=new HashMap<String,String>();

ArrayList<String> YourNames=new ArrayList<String>();
mx.add("Ashvin");
mx.add("solanki");
mx.add("Tank");

hm.put("Ashvin","Amit");
hm.put("Tank","Vijay");
hm.put("Solanki","Rahul");

Accessing All Value:

for(Map.Entry m:hm.entrySet())
{
    System.out.println(m.getKey()+" "+m.getValue());
}

System.out.println("----------New OutPut---------")

Check Hasmap Key same As Your Name Or Not

For Example:

hm.get(x);

x contain Yourname like XYZ if Hasmap has this key it returns some value else it return NULL value

for(int i=0;i<YourNames.size();i++)
{
    for(int j=0;j<hm.size();j++)
    {
         if(hm.get(mx.get(i))!=null)
         {
             System.out.println(hm.get(YourNames.get(i))+" ");
             break;
          }
     }
}

---------------------OutPut----------------------

Ashvin Amit
Solanki Rahul
Tank Vijay
----------New OutPut---------
Amit 
Vijay 
Community
  • 1
  • 1
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
1

A convienient solution for your desired behaviour is a map. Taking the above example you can build a construct like:

String[] myArray = new String[2];
myArray[ 0 ] = "string1";
myArray[ 1 ] = "string2";

Map<String, int[]> mapper = new HashMap<>();
mapper.put( myArray[ 0 ], new int[10] );
mapper.put( myArray[ 1 ], new int[25] );
// and so on

then you can access the int arrays by calling:

mapper.get( "string1" );

for example:

System.out.println( mapper.get( "string1" ).length );

would output:

10

L.Spillner
  • 1,772
  • 10
  • 19