Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Use 2 dimensional array to instantiate new objects

Thread view: 
eomer - 01 Dec 2005 21:25 GMT
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.
Roedy Green - 01 Dec 2005 21:39 GMT
>Permutation

see http://mindprod.com/jgloss/permutation.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Oliver Wong - 01 Dec 2005 21:46 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> 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 - 01 Dec 2005 22:04 GMT
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.

> > Hi,
> >
[quoted text clipped - 19 lines]
>
>     - Oliver
Oliver Wong - 01 Dec 2005 22:21 GMT
> Hi Oliver,
> What I can't figure out is the actual creation of the 3 objects using
[quoted text clipped - 3 lines]
>
> 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
eomer - 03 Dec 2005 01:02 GMT
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[i] = new Permutations(vals[i][]);
            }
        } // end of main
    } // end of class TestPermutations
Roedy Green - 03 Dec 2005 03:25 GMT
>numbers[i] = new Permutations(vals[i][]);

you constructor wants three separate values.  You are feeding it a
slice of your matrix, i.e. muliple values in a single parameter..
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

eomer - 03 Dec 2005 13:03 GMT
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[i].length; j++) {
                    if (j == 0) tempa = vals[i][j];
                    if (j == 1) tempb = vals[i][j];
                    if (j == 2) tempc = vals[i][j];
                } // end inner for
                numbers[i] = new Permutations(tempa, tempb, tempc);
            } // end outer for
Oliver Wong - 05 Dec 2005 15:51 GMT
> thank you for your time. I came up with the solution due to your
> suggestion.
[quoted text clipped - 7 lines]
> numbers[i] = 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[i] = new Permutations(vals[i][0], vals[i][1], vals[i][2]);
}

   - Oliver
eomer - 05 Dec 2005 21:01 GMT
Oliver,

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

Again, thanks.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.