Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Passing Value to use as Member Veritable Array Dimension.

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Passing Value to use as Member Veritable Array Dimension.

    Hey, is there any way to pass a value to a object and then use it as the dimension for a Member Variable Array.

    Basically I want to be able to pass a integer to a object on its creation, and then use it as the dimension value for an array that can be accessed by all of the method within that class.

    Or is there some way to make an Array declared inside a method or constructor global to all methods within that class?

    Like the below code...

    public class TestExample {
    	long some_array[][];
     
    	public TestExample(int some_int){
    		some_array[][] = new long[some_int][4];
    	}
     
     
    }


    --- Update ---

    I still interesting if there is a simple way to do this and have just one array... but I just solved it by using 3 ArrayLists and then writing methods so they act like one big array.

    import java.util.ArrayList;
     
    public class Files {
        long somearray[][];
     
        ArrayList<Long> listid = new ArrayList<Long>();
        ArrayList<Long> listamount = new ArrayList<Long>();
        ArrayList<Long> listdownloded = new ArrayList<Long>();
     
        public Files(){
     
        }
     
        public void setmap(int x, int y, long value){
            if (y == 0){
                if (listid.contains(x)){
                    listid.set(x, value);
                }
                else listid.add(value);
            }
            if (y == 1){
                if (listamount.contains(x)){
                    listamount.set(x, value);
                }
                else listamount.add(value);
            }
            if (y == 2){
                if (listdownloded.contains(x)){
                    listdownloded.set(x, value);
                }
                else listdownloded.add(value);
            }
        }
     
        public long map(int x, int y){
     
            long fail = -1;
     
            if (y == 0){
                return listid.get(x);
            }
            else if (y == 1){
                return listamount.get(x);
            }
            else if (y == 2){
                return listdownloded.get(x);
     
            }
     
            return fail;
     
        }
     
        public int maplength(){
            return listid.size();
        }
     
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Passing Value to use as Member Veritable Array Dimension.

    make an Array declared inside a method or constructor global to all methods within that class?
    Not possible.
    Anything declared inside a method is local to that method.

    Like the below code...
    That example shows some_array is declared at the class level, not inside of any method.
    It can be assigned a value inside of any method.

    using 3 ArrayLists and then writing methods so they act like one big array.
    Why not define a class to hold the 3 values and then store instances of the class in one ArrayList?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do i copy this single dimension array?
    By cgskook in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 6th, 2013, 11:06 PM
  2. Passing an array to the constructor!
    By Mike8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2013, 12:21 PM
  3. Single dimension array to multidimension array
    By 07.350 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 7th, 2013, 09:03 PM
  4. error: array dimension missing x 2. Any help?
    By giga in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 30th, 2012, 09:50 AM
  5. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM