Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 4th October 2008, 08:02
CMYK CMYK is offline
Junior Member
 
Join Date: Sep 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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?
Reply With Quote
Sponsored Links
  #2  
Old 6th October 2008, 06:59
CMYK CMYK is offline
Junior Member
 
Join Date: Sep 2008
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

[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.
Reply With Quote
  #3  
Old 6th October 2008, 14:22
burschik burschik is offline
Junior Member
 
Join Date: May 2008
Posts: 24
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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.
Reply With Quote
  #4  
Old 17th October 2008, 18:57
WayneKan1111 WayneKan1111 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 2 Times in 2 Posts
Default

check here to better understand how to make a new name for an array.
hth
Reply With Quote
The Following User Says Thank You to WayneKan1111 For This Useful Post:
CMYK (18th October 2008)
Reply

Bookmarks

Tags
array, dynamic allocation, java

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cast a char ptr to an array (to prevent warning, it does work) Owen_Townsend Programming/Scripts 0 4th September 2008 01:12
Debian: Failed Raid5 Array & Kernal Panic snowfly Installation/Configuration 0 29th August 2007 12:52
Warning: fwrite(): supplied argument is not a valid stream resource Salvis General 7 4th July 2007 15:43
log files, rotation and emailing them hairydog2 General 65 11th October 2006 13:18
phpMyAdmin - panick attack md@waw.co.uk General 6 3rd September 2006 20:52


All times are GMT +2. The time now is 09:40.


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