Use 2 dimensional array to instantiate new objects

E

eomer

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.
 
O

Oliver Wong

eomer said:
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
 
E

eomer

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 said:
eomer said:
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
 
O

Oliver Wong

eomer said:
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
 
E

eomer

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
 
R

Roedy Green

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..
 
E

eomer

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
 
O

Oliver Wong

eomer said:
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
 
E

eomer

Oliver,

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

Again, thanks.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top