HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   array allocation question in java (http://www.howtoforge.com/forums/showthread.php?t=27429)

CMYK 4th October 2008 09:02

array allocation question in java
 
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
Code:

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?

CMYK 6th October 2008 07:59

[QUOTE=a_kanna230;148690]Java is a programming language originally developed by Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture.

__________________________________________________ ____________


Uhh thanks but I already know about JVM, bytecode, and Java history. It's kind of irrelevant to my question but thanks for the refresher.

burschik 6th October 2008 15:22

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.

WayneKan1111 17th October 2008 19:57

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


All times are GMT +2. The time now is 11:57.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.