Jump to content

Need to know the answer to this question

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
ToMegaTherion

ToMegaTherion

    Newbie

  • Members
  • PipPip
  • 29 posts
Ok, i'm working on an assignment for uni which is essentially just to write a program in Java.

Basically all I want to know is if it is possible to use a variable as a variable definition. So something like

if ( some prerequisite )

    int n;

    n = 1;

    do {

        int (n);

        // more statements to fill n

        n++;

    } while (some condition);


Basically I want to know if this is even possible in Java because I'm not sure I havn't tested it yet but I will. Second if it is possible can some offer just a general outline something like what I've done above so I can see how it might be done.

#2
ToMegaTherion

ToMegaTherion

    Newbie

  • Members
  • PipPip
  • 29 posts
Oh by the way I'm aware I could do something like this probably easier using an array. I'm doing this because I don't know how many results I am going to need for int (n), where n is the predefined variable. until I have tested some data, then I need to be able to record the result to display in sequence later (where int (n)) is the data test results for display.

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
First of all, variable names in Java can't begin with a number.

Quote

Basically all I want to know is if it is possible to use a variable as a variable definition
I never saw that happen in Java before. It is possible to get the value of a variable by just having the name, with reflection. But that's very ugly code.

For cases like yours, a Map is often used. Example:

import java.util.HashMap;

public class Test {
    HashMap<Integer, Integer> intMap;
    int[] intArray;

    public Test() {
        intMap = new HashMap<Integer, Integer>();
    }

    private void fillHashMap(){
        for(int n=0; n<10 ; n++){
            intMap.put(n, n*24);
        }
    }

    private void createArray(){
        intArray = new int[intMap.size()];
        for(int i=0 ; i<intArray.length ; i++){
            intArray[i] = intMap.get(i);
        }
    }

    private void printArray(){
        for(int number : intArray){
            System.out.println(number);
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.fillHashMap();
        test.createArray();
        test.printArray();
    }
}

Quote

Oh by the way I'm aware I could do something like this probably easier using an array. I'm doing this because I don't know how many results I am going to need for int (n), where n is the predefined variable. until I have tested some data, then I need to be able to record the result to display in sequence later (where int (n)) is the data test results for display.

If the only reason you're doing this is that you don't know the size of the array up front then use a dynamic array. The most used collection type in Java is probably the ArrayList. There is no need to predefine the size of it, as it will automatically expand.
ArrayList myList = new ArrayList<Integer>();
myList.add(2);
myList.add(15);
myList.add(3);

for(int i=0 ; i<myList.size() ; i++){
  System.out.print(myList.get(i) + " ");
}




#4
ToMegaTherion

ToMegaTherion

    Newbie

  • Members
  • PipPip
  • 29 posts
Yeh I played with it for a while and decided that it was pretty messy. So in the end I found another way of doing it be basically creating a class with an array to collect the data from a method whose index is passed by the main function. In the end I found a solution which was much more viable and a heck of a lot less work. But this is what programming is about for beginners like me. Stuff around if it doesn't work try and try again.