Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Use 2 dimensional array to instantiate new objects

Reply
Thread Tools

Use 2 dimensional array to instantiate new objects

 
 
eomer
Guest
Posts: n/a
 
      12-01-2005
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.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-01-2005
On 1 Dec 2005 13:25:31 -0800, "eomer" <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>Permutation


see http://mindprod.com/jgloss/permutation.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      12-01-2005

"eomer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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


 
Reply With Quote
 
eomer
Guest
Posts: n/a
 
      12-01-2005

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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> > 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


 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      12-01-2005

"eomer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> 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


 
Reply With Quote
 
eomer
Guest
Posts: n/a
 
      12-03-2005
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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-03-2005
On 2 Dec 2005 17:02:40 -0800, "eomer" <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>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..
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
eomer
Guest
Posts: n/a
 
      12-03-2005
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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      12-05-2005

"eomer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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
>


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


 
Reply With Quote
 
eomer
Guest
Posts: n/a
 
      12-05-2005
Oliver,

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

Again, thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To convert a one dimensional array into a two dimensional array using C amrutha0303 Software 0 08-03-2010 10:02 PM
how to instantiate array of objects knowing its indices at compiletime? Christof Warlich C++ 4 08-09-2008 09:22 PM
instantiate new objects Felix Steffenhagen Python 2 03-10-2005 11:54 PM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM
Re: Two dimensional pointers and Two dimensional arrays Alf P. Steinbach C++ 0 08-18-2003 08:25 AM



Advertisments