Use 2 dimensional array to instantiate new objects

Discussion in 'Java' started by eomer, Dec 1, 2005.

  1. eomer

    eomer Guest

    Hi,

    I have a 2 dimensional array of integers:

    int vals[][] = {{1,2,3,4,5},{1,2,3,5,4},{1,2,4,3,5};. This array would
    be in my main class.

    Assume that I have a class named Permutation. Each object of type
    Permutation would have 5 integers passed to it upon instantiation and
    then do something with those 5 ints.

    My question is how do I use the vals[][] array to instantiate 3 new
    objects of type Permutation?

    Thanks in advance for your help.
     
    eomer, Dec 1, 2005
    #1
    1. Advertising

  2. eomer

    Roedy Green Guest

    Roedy Green, Dec 1, 2005
    #2
    1. Advertising

  3. eomer

    Oliver Wong Guest

    "eomer" <> wrote in message
    news:...
    > Hi,
    >
    > I have a 2 dimensional array of integers:
    >
    > int vals[][] = {{1,2,3,4,5},{1,2,3,5,4},{1,2,4,3,5};. This array would
    > be in my main class.
    >
    > Assume that I have a class named Permutation. Each object of type
    > Permutation would have 5 integers passed to it upon instantiation and
    > then do something with those 5 ints.
    >
    > My question is how do I use the vals[][] array to instantiate 3 new
    > objects of type Permutation?


    "vals" is an array of length 3. The elements of this array are
    themselves array. Coincidentally, all those arrays happen to have length 5,
    and they happen to contain ints.

    Presumably, since your Permutation constructor expects 5 ints, you're
    going to be making 3 such Permutations. Just give each permutation one of
    those 3 arrays of ints.

    - Oliver
     
    Oliver Wong, Dec 1, 2005
    #3
  4. eomer

    eomer Guest

    Hi Oliver,
    What I can't figure out is the actual creation of the 3 objects using
    this array. Somehow, I need to go through the entire array and do the
    instantiation stuff (Permuatation temp = new Permutation()). I just
    don't know how to do this with the 2 dimensional array.

    Thanks.


    Oliver Wong wrote:
    > "eomer" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I have a 2 dimensional array of integers:
    > >
    > > int vals[][] = {{1,2,3,4,5},{1,2,3,5,4},{1,2,4,3,5};. This array would
    > > be in my main class.
    > >
    > > Assume that I have a class named Permutation. Each object of type
    > > Permutation would have 5 integers passed to it upon instantiation and
    > > then do something with those 5 ints.
    > >
    > > My question is how do I use the vals[][] array to instantiate 3 new
    > > objects of type Permutation?

    >
    > "vals" is an array of length 3. The elements of this array are
    > themselves array. Coincidentally, all those arrays happen to have length 5,
    > and they happen to contain ints.
    >
    > Presumably, since your Permutation constructor expects 5 ints, you're
    > going to be making 3 such Permutations. Just give each permutation one of
    > those 3 arrays of ints.
    >
    > - Oliver
     
    eomer, Dec 1, 2005
    #4
  5. eomer

    Oliver Wong Guest

    "eomer" <> wrote in message
    news:...
    >
    > Hi Oliver,
    > What I can't figure out is the actual creation of the 3 objects using
    > this array. Somehow, I need to go through the entire array and do the
    > instantiation stuff (Permuatation temp = new Permutation()). I just
    > don't know how to do this with the 2 dimensional array.
    >
    > Thanks.


    You have to get the information of the five numbers into the Permutation
    class somehow. The most straightforward way I can think of is to pass them
    in the constructor. The second most straightforward way I can think of is to
    pass them into a setter method of some sort.

    What does the interface of Permutation look like, and are you allowed to
    modify it?

    - Oliver
     
    Oliver Wong, Dec 1, 2005
    #5
  6. eomer

    eomer Guest

    Hi Oliver,

    This is what I have so far. The Permutations class doesn't do anything
    yet. I just want to get past the initial compile errors when attempting
    to create the 3 objects and then 'store' them in an array of
    Permutations objects. This code is in 2 files.

    public class Permutations {
    int val1 = 0;
    int val2 = 0;
    int val3 = 0;

    Permutations(int x, int y, int z) {
    val1 = x;
    val2 = y;
    val3 = z;
    }

    } // end of class Permutations


    *****************************************************************************************************************
    public class TestPermutations {
    public static void main (String argv[]) {

    int vals [][] = {{1,2,3}, {4,5,6}, {7,8,9}};
    Permutations[] numbers = new Permutations[3];

    for (int i = 0; i < vals.length; i++) {
    numbers = new Permutations(vals[]);
    }
    } // end of main
    } // end of class TestPermutations
     
    eomer, Dec 3, 2005
    #6
  7. eomer

    Roedy Green Guest

    On 2 Dec 2005 17:02:40 -0800, "eomer" <> wrote,
    quoted or indirectly quoted someone who said :

    >numbers = new Permutations(vals[]);


    you constructor wants three separate values. You are feeding it a
    slice of your matrix, i.e. muliple values in a single parameter..
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 3, 2005
    #7
  8. eomer

    eomer Guest

    thank you for your time. I came up with the solution due to your
    suggestion.

    for (int i = 0; i < vals.length; i++) {
    for (int j = 0; j < vals.length; j++) {
    if (j == 0) tempa = vals[j];
    if (j == 1) tempb = vals[j];
    if (j == 2) tempc = vals[j];
    } // end inner for
    numbers = new Permutations(tempa, tempb, tempc);
    } // end outer for
     
    eomer, Dec 3, 2005
    #8
  9. eomer

    Oliver Wong Guest

    "eomer" <> wrote in message
    news:...
    > thank you for your time. I came up with the solution due to your
    > suggestion.
    >
    > for (int i = 0; i < vals.length; i++) {
    > for (int j = 0; j < vals.length; j++) {
    > if (j == 0) tempa = vals[j];
    > if (j == 1) tempb = vals[j];
    > if (j == 2) tempc = vals[j];
    > } // end inner for
    > numbers = new Permutations(tempa, tempb, tempc);
    > } // end outer for
    >


    That probably works, but it seems like your if-statements are assuming
    that the size of the inner array is 3. (i.e. that j goes from 0 to 2). If
    that's the case, then maybe you shouldn't be using a for-loop in that
    situation. Try something like:

    for (int i = 0; i <vals.length; i++) {
    numbers = new Permutations(vals[0], vals[1], vals[2]);
    }

    - Oliver
     
    Oliver Wong, Dec 5, 2005
    #9
  10. eomer

    eomer Guest

    Oliver,

    Thanks for the reply. Your suggestion is much cleaner looking than all
    the extra 'if' statements I have.

    Again, thanks.
     
    eomer, Dec 5, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    444
    Alf P. Steinbach
    Aug 18, 2003
  2. Venkat
    Replies:
    4
    Views:
    994
    Venkat
    Dec 5, 2003
  3. Felix Steffenhagen

    instantiate new objects

    Felix Steffenhagen, Mar 10, 2005, in forum: Python
    Replies:
    2
    Views:
    407
    Felix Steffenhagen
    Mar 10, 2005
  4. Replies:
    5
    Views:
    728
    David Harmon
    Jan 6, 2007
  5. Christof Warlich
    Replies:
    4
    Views:
    318
    Christof Warlich
    Aug 9, 2008
Loading...

Share This Page