Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Question: Variable name + integer  (Read 1826 times)
0 Members and 1 Guest are viewing this topic.
Offline kuldar

Senior Newbie





« Posted 2008-03-30 15:46:28 »

Hey! Im hoping that this is the right place to post my question, since im fairly new to this forum. Anyways, I was wondering if it was possible to make variables/instances with a loop in java or some other dynamical variable creation method. For example:
for(int i = 0; i < 10; i++)
{
    Variable var+i = new Variable();
}

And in the end i would have:

var0, var1..... var9
Offline Eatserbeam

Junior Newbie





« Reply #1 - Posted 2008-03-30 16:07:43 »


  Well arrays  are exactly wath you need,
                                                   heres a example code:

                     public class Arrayer {

                                   private int number_Of_Numbers = 9;
                                   private int[] number = new int[number_Of_Numbers];    // or just new int[9]
 
                                                     for (int i; i < number_Of_Numbers; i++){
                                                                       number = i                                         // making the array number[0] = 0, number[1] = 1, etc
                                                         }
                                                                      //  so you can use them like
                                                                                  number[3] = 5;
                                                                                  number[2]++;
                                              }
   
 But arrays always starts with cero, so int[] array = new int[5], will make 5 ints starting with array[0] and ending with array[4]
you can make any object array, you can make Button[] Butt = new Button[4], making 4 buttons, and in the init method a for loop is used to initialize them - for(int r; r < 4, r++){ Button[r] = new Button("button"); Button[r].addActionListener(this); add(Button[r]); }
...

so your code will look like{

int[] var = new int[9];

       for(int i = 0; i < 9; i++)
{
    var = 1;                              // making all variables value 1
}
        then you will have  var[0], var[1], ........., var[9];

      Google for more Java arrays information Smiley
                      i Hope this helped you
Offline kuldar

Senior Newbie





« Reply #2 - Posted 2008-03-30 16:48:41 »

Array is not what i had in mind, since it is static. Ie: If i make array of 5, it will stay that way, even if the WWIII breaks out. However I need something that can change during the execution of application. Ie: User wants to add elements (ex: number of enemies) ->
not all users will set it to 200, so i would not want to waste resources on making an array of 200 elements.

Edit: Ty anyways for replying.  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jackal von ÖRF

Junior Devvie





« Reply #3 - Posted 2008-03-30 16:53:09 »

Use ArrayList or some other collection: http://java.sun.com/javase/6/docs/api/java/util/package-summary.html

Offline kuldar

Senior Newbie





« Reply #4 - Posted 2008-03-30 18:19:23 »

Ty! Im trying to use Vector, however there is a slight problem:

Vector vec = new Vector();
vec.add(0, new Foo(param, param, param));

Is there a way to use Foos methods ie: (vec.get(0)).update(); ?
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 833
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2008-03-30 18:56:55 »

Foo f = (Foo)v.get(0);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline kuldar

Senior Newbie





« Reply #6 - Posted 2008-03-30 19:22:43 »

Ok that worked, ty. Smiley
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #7 - Posted 2008-03-30 21:38:06 »

Preferably use generics for the Vector declaration.
e.g.

1  
Vector<Foo> vec = new Vector<Foo>()

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #8 - Posted 2008-03-31 07:15:57 »

Vector is synchronized, so it's (usually) slower than ArrayList. Don't use Vector.

Play Minecraft!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (51 views)
2014-11-26 15:22:04

toopeicgaming1999 (43 views)
2014-11-26 15:20:36

toopeicgaming1999 (8 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (25 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!