Jump to content

passing data from one Object array to another Object Array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
hariza

hariza

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#2
Fae

Fae

    Learning Programmer

  • Members
  • PipPipPip
  • 80 posts
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:

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
hariza

hariza

    Newbie

  • Members
  • Pip
  • 3 posts

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:


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
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
The System class has a static method "arraycopy"
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.