Hi Again,
Actually I could be asking a silly question but here I go. I'm creating a array of objects and filling it in with the piece of code below store in a class:
while (rs.next()) {
String a1 = rs.getString(1);
String a2 = rs.getString(2);
String a3 = rs.getString(3);
String a4 = rs.getString(4);
String a5 = rs.getString(5);
data1 = new Object[][] {{a1,a2,a3,a4,a5}};
}
Then now I need to pass this array of objects to another array in a different class
Object data[][] = new Object [10][];
then I need to basically make copy the data from data1 into data. Please your help is highly appreciated. thanks.
passing data from one Object array to another Object Array
Started by hariza, Aug 20 2010 09:18 PM
3 replies to this topic
#1
Posted 20 August 2010 - 09:18 PM
|
|
|
#2
Posted 21 August 2010 - 01:32 AM
Hi again :)
I'm not quite sure what the difficulty you're having is. So, here's a general overview of how I would do it (Whichmay not be the best way, remember, I'm just learning it all myself!), how this helps.
Design it so that the class retrieving the data (Class 1) is just a tool used by the class that you're passing the data to (Class 2)
Class 1's method retrieves the values as you've written in your post, and has a return type of Object[][]. For example:
Your other class (Class 2) creates an instance of Class1, and then calls the relevant method when assigning value to the array
...then, your Class2 should receive the Object array in data[][].
So, basically, you're usingh Class1 for data retrieval, which is used by Class2 to get the data so Class2 can do whatever with it.
I think that's what you were asking...?
Anyways, hope this helps.
~Fae
I'm not quite sure what the difficulty you're having is. So, here's a general overview of how I would do it (Whichmay not be the best way, remember, I'm just learning it all myself!), how this helps.
Design it so that the class retrieving the data (Class 1) is just a tool used by the class that you're passing the data to (Class 2)
Class 1's method retrieves the values as you've written in your post, and has a return type of Object[][]. For example:
public Object[][] getArray (args) {}
while (rs.next()) {
String a1 = rs.getString(1);
String a2 = rs.getString(2);
String a3 = rs.getString(3);
String a4 = rs.getString(4);
String a5 = rs.getString(5);
data1 = new Object[][] {{a1,a2,a3,a4,a5}};
return data1;
Your other class (Class 2) creates an instance of Class1, and then calls the relevant method when assigning value to the array
Class1 arrayRetrieval = new Class1() Object[][] data = arrayRetrieval.getArray();
...then, your Class2 should receive the Object array in data[][].
So, basically, you're usingh Class1 for data retrieval, which is used by Class2 to get the data so Class2 can do whatever with it.
I think that's what you were asking...?
Anyways, hope this helps.
~Fae
I'll ask a lot of questions (most of them probably stupid stuff). Bear with me, i'm still learning! ^_^ Also, I'll try to answer as many questions as I can as well, but I'm not very good yet. I'm sure I'll be of more use once I get better :)
#3
Posted 21 August 2010 - 01:42 AM
Fae said:
Hi again :)
I'm not quite sure what the difficulty you're having is. So, here's a general overview of how I would do it (Whichmay not be the best way, remember, I'm just learning it all myself!), how this helps.
Design it so that the class retrieving the data (Class 1) is just a tool used by the class that you're passing the data to (Class 2)
Class 1's method retrieves the values as you've written in your post, and has a return type of Object[][]. For example:
Your other class (Class 2) creates an instance of Class1, and then calls the relevant method when assigning value to the array
...then, your Class2 should receive the Object array in data[][].
So, basically, you're usingh Class1 for data retrieval, which is used by Class2 to get the data so Class2 can do whatever with it.
I think that's what you were asking...?
Anyways, hope this helps.
~Fae
I'm not quite sure what the difficulty you're having is. So, here's a general overview of how I would do it (Whichmay not be the best way, remember, I'm just learning it all myself!), how this helps.
Design it so that the class retrieving the data (Class 1) is just a tool used by the class that you're passing the data to (Class 2)
Class 1's method retrieves the values as you've written in your post, and has a return type of Object[][]. For example:
public Object[][] getArray (args) {}
while (rs.next()) {
String a1 = rs.getString(1);
String a2 = rs.getString(2);
String a3 = rs.getString(3);
String a4 = rs.getString(4);
String a5 = rs.getString(5);
data1 = new Object[][] {{a1,a2,a3,a4,a5}};
return data1;
Your other class (Class 2) creates an instance of Class1, and then calls the relevant method when assigning value to the array
Class1 arrayRetrieval = new Class1() Object[][] data = arrayRetrieval.getArray();
...then, your Class2 should receive the Object array in data[][].
So, basically, you're usingh Class1 for data retrieval, which is used by Class2 to get the data so Class2 can do whatever with it.
I think that's what you were asking...?
Anyways, hope this helps.
~Fae
Thanks for your reply. I really appreciated. As you can the level of my question my java skills are pretty poor. I just decided to to start rewritting some of our inhouse scripts into java so I can practise and learn more and also offer a more user friendly solutions to out customers. I'll try your suggestion and let you know. Thanks again.
#4
Posted 21 August 2010 - 01:59 AM
The System class has a static method "arraycopy"
Example:
Example:
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String[][] source = new String[][] {{"1","2","3","4","5"}};
String[][] destination = new String[10][];
System.arraycopy(source, 0, destination, 0, 1);
for(int i=0 ; i<5 ; i++)
System.out.println(destination[0][i]);
}
}
output prints 1 to 5 from the destination array.


Sign In
Create Account

Back to top









