array allocation question in java

Discussion in 'Programming/Scripts' started by CMYK, Oct 4, 2008.

  1. CMYK

    CMYK New Member

    In my source code for a mathematical project and I'm running into a problem that ends with a duplicate variable names error. In my function I take a string as an argument to be the name of a new array that will be allocated. Here's what the source code looks like
    private static void somefunction(String name) {
    int name[] = new int[10];
    I need to call that function at least twice and I think that the compiler is getting confused and tries to allocate an array by the name of name instead of the string I provide as an argument. When it gets called the second time it tries to allocate name[] again and javac raises the error.
    Does anyone know how I could tell Java to name the array what I provide as an argument?
  2. CMYK

    CMYK New Member

  3. burschik

    burschik New Member

    I'm afraid it is you rather than the compiler who is confused. Of course the array is called "name". That you passed a String variable also called "name" as an argument to the method is irrelevant. You could probably do what you want to do by using reflection, but I think it is highly unlikely that it would be worth the trouble.

    Of course, I have no idea what you are trying to achieve, but you might consider passing the array itself as an argument to the method, or using a Map<String, ArrayList> or something like that instead.
  4. WayneKan1111

    WayneKan1111 New Member

    check here to better understand how to make a new name for an array.

Share This Page