Results 1 to 9 of 9
  1. #1
    ce3c is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default Accessing array in other class

    Hello,

    I'm making my first Java app, and I came across this problem today:
    I'd like to access
    DB1.database[0] DB2.database[0] DB3.database[0] (etc..) based on an integer i.

    {"DB".i}.database[0] doesn't seem to work tho. What is the correct format to do this?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You can create a public getter method in the class that holds the array and then have the other class call that public method on an object of the first class (again, the one that holds the array).

    By the way, can you clarify this statement here?:
    DB1.database[0] DB2.database[0] DB3.database[0] (etc..) based on an integer i.

  3. #3
    BeholdMyGlory is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by ce3c View Post
    Hello,

    I'm making my first Java app, and I came across this problem today:
    I'd like to access
    DB1.database[0] DB2.database[0] DB3.database[0] (etc..) based on an integer i.

    {"DB".i}.database[0] doesn't seem to work tho. What is the correct format to do this?
    You could store the DBs in an array, as such:
    Java Code:
    DB[] dbArray = new DB[3];
    dbArray[0] = new DB();
    dbArray[1] = new DB();
    dbArray[2] = new DB();
    System.out.println(dbArray[2].database[0]);
    This is though assuming that you have a class named DB.
    Did that make much sense to you?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah, I think I see now. Behold's explanation seems to be spot on. Please ignore what I posted previously.

  5. #5
    ce3c is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    DB[] dbArray = new DB[3];
    dbArray[0] = new DB();
    dbArray[1] = new DB();
    dbArray[2] = new DB();
    System.out.println(dbArray[2].database[0]);
    I've got 108 class files... putting "dbArray[*] = new DB();" each time doesn't seem the good way to me.

    DB1.java
    DB2.java
    DB3.java
    etc...

    DB1.java contains this:
    Java Code:
    class DB1
    extends Test
    {
    	public static String[][] database = {
    		{"a", "text sfkejfle kjsekfj sekfj sk"},
    		{"b", "fjkjefk esfkje fksejf sjefksekj"}
    	};
    }
    I need to split my classes up over 108 files because Java doesn't allow files over 65535 characters. I'm using (huge) arrays as a database to make some sort of a dictionary in J2ME.

    So what I would like to do in fact (in my main Test.java file) is this:

    Java Code:
    for (int i=1; i<109; i++) {
    	helloForm.append({"DB"+i}.database[0][0]);
    }
    I tried to concat every DB*.database (which doesn't seem possible because of the 65535 char limit).

    Sorry, it really is one of my first days Java. I'm used to PHP (that's why I'm writing {"DB"+i} which would be similar..)
    Last edited by ce3c; 12-26-2008 at 12:45 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've got 108 class files... putting "dbArray[*] = new DB();" each time doesn't seem the good way to me.
    Of course you wouldn't do that. You'd use a for loop instead.

    Java is very different from PHP and you can't just jump from one type of programming to the other. For instance variable names are not so flexible in Java so what you are trying to do to manipulate them is doomed to fail.

    Strong word of advice: stop doing your project now and go through a text book and / or tutorial on Java. If not, you are going to be writing a monstrously large program that you will just have to discard later as it will be impossible to maintain or debug.
    Last edited by Fubarable; 12-26-2008 at 01:06 AM.

  7. #7
    BeholdMyGlory is offline Member
    Join Date
    Dec 2008
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by ce3c View Post
    Java Code:
    DB[] dbArray = new DB[3];
    dbArray[0] = new DB();
    dbArray[1] = new DB();
    dbArray[2] = new DB();
    System.out.println(dbArray[2].database[0]);
    I've got 108 class files... putting "dbArray[*] = new DB();" each time doesn't seem the good way to me.

    DB1.java
    DB2.java
    DB3.java
    etc...

    DB1.java contains this:
    Java Code:
    class DB1
    extends Test
    {
    	public static String[][] database = {
    		{"a", "text sfkejfle kjsekfj sekfj sk"},
    		{"b", "fjkjefk esfkje fksejf sjefksekj"}
    	};
    }
    I need to split my classes up over 108 files because Java doesn't allow files over 65535 characters. I'm using (huge) arrays as a database to make some sort of a dictionary in J2ME.

    So what I would like to do in fact (in my main Test.java file) is this:

    Java Code:
    for (int i=1; i<109; i++) {
    	helloForm.append({"DB"+i}.database[0][0]);
    }
    I tried to concat every DB*.database (which doesn't seem possible because of the 65535 char limit).

    Sorry, it really is one of my first days Java. I'm used to PHP (that's why I'm writing {"DB"+i} which would be similar..)
    Wow. That seems impractical. Wouldn't it be possible to have one single class, called "DB", which takes a String array like this:

    Java Code:
    class DB
    extends Test
    {
    	public String[][] database; //note the removal of static
    
            public DB(String[][] sArr) {
                   database = sArr;
            }
    }
    And then create an object like this:

    Java Code:
    String[][] db = {
    		{"a", "text sfkejfle kjsekfj sekfj sk"},
    		{"b", "fjkjefk esfkje fksejf sjefksekj"}
    	};
    DB object = new DB(db);
    
    String[] tmp = object.database[0];
    or something like that. But what you could do is to store all the data as normal text, and then read from the text file in the program itself. Perhaps creating an array of DB objects based on what's in the text file.

  8. #8
    ce3c is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Java Code:
    Perhaps creating an array of DB objects based on what's in the text file.
    At the moment it is a J2ME program, I'm building it for my mobile phone. Accessing its filesystem is way more complicated, and the data wouldn't be compressed like in the .JAR which the compiler produces.

    Isn't there a function like StringToClassName() ?

    I'll try to put everything into a different entry of an array, but I'm afraid the 65535 charlimit-error will rise again.

  9. #9
    ce3c is offline Member
    Join Date
    Dec 2008
    Posts
    4
    Rep Power
    0

    Default

    Bumping up this old thread..
    been away a while but found a solution to my problem now.
    For other noobies who have the same problem:

    I didn't know the class name I wanted to load before the app was executed.
    The solution was (quite) simple though:

    If you would like to "DB1.class" or "DB2.class" depending on an int,
    this is the way:

    Java Code:
    try {
    	Class.forName("DB1").newInstance(); 
    	// or Class.forName("DB"+int).newInstance();   where int=1;
    	// equals "new DB1();"
    } catch (InstantiationException e) {
    	e.printStackTrace();
    } catch (IllegalAccessException e) {
    	e.printStackTrace();
    } catch (ClassNotFoundException e) {
    	e.printStackTrace();
    }

Similar Threads

  1. Accessing list out another class
    By Preethi in forum New To Java
    Replies: 23
    Last Post: 10-26-2008, 02:54 PM
  2. Replies: 0
    Last Post: 02-17-2008, 09:03 AM
  3. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  4. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 AM
  5. Replies: 5
    Last Post: 01-03-2008, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •