How to get multiple return value

hi all!I have a method to get some values and put them in 3 arrays which include both one and two dimension arrays. I used String [ ] and String [ ]. The problem is how could I get the values in both String [ ] and String [ ] from outside the method`?Thanks a lot !
[286 byte] By [Linzia] at [2007-10-3 2:52:12]
# 1

It all depends on exactly what you're using the String[] and String[][] for. You could possible extend the String[] to a String[][] with only 1 initial element (so instead of doing String[x] you would do String[0][x] on that one), and then put both of the String[][] in an ArrayList... but depending on what you're doing with them once you have them, that may not be the best solution.

technodolta at 2007-7-14 20:41:08 > top of Java-index,Java Essentials,Java Programming...
# 2

> hi all!

>

> I have a method to get some values and put them in 3

> arrays which include both one and two dimension

> arrays. I used String [ ] and String [ ]. The problem

> is how could I get the values in both String [ ] and

> String [ ] from outside the method`?

>

> Thanks a lot !

What exactly are you trying to do b/c it sure sounds like you're going about it the wrong way.

Norweeda at 2007-7-14 20:41:08 > top of Java-index,Java Essentials,Java Programming...
# 3

Generally speaking, when you need to return multiple values from a method, it falls into one of two situations:

1) the multiple values correspond with each other in some way, for example "customer name" and "customer phone number". In this situation, it's usually best to return an object that encapsulates this pairing (e.g., a Customer object). It's also a red flag that maybe the caller code should be folded into the class of the called code if they're not already part of the same class -- it could be a violation of encapsulation.

2) the multiple values are multiple instances of the same kind of information, for example "customer #1 name", "customer #2 name", "customer #3 name". In this case, it's usually best to return a java.util.Collection holding multiple objects.

But as Norweed pointed out, it sounds like you're doing something the wrong way.What you're doing sounds unduly complicated to begin with. But also when you're creating complicated array structures, it's usually a sign that you're doing something wrong, especially in an OO language like Java.

paulcwa at 2007-7-14 20:41:08 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks!

Seems I am on the wrong way. What I am doing is like this example of a picture (arrays used are either String[ ] or String [ ][ ] in following):

A array Box_array[ ] stores all the names of boxes. Box A (called Source Box) points to Box B (Destination Box) by a directed line called AB. The joints are called Aab on Box A and Bab on Box B respectively. Similarly, Box A may point to another Box C. Box B also may point to Box D. So I use arrary Src_arrary[ k ] to store the name of one Souce Boxe,and Dst_arrary [k ][ ] to store the corresponding Destination Boxes. E.g.,

Src_array[0] = "A";

Src_array[1] = "B";

Dst_array[0][0] = "B";

Dst_array[0][1] = "C";

Dst_array[1][0] = "D";

In the same way,

Line_array[0][0] ="AB";Point_Src[0][0] = "Aab";Point_Dst[0][0]= "Bab";

Line_array[0][1] ="AC";Point_Src[0][1] = "Aac";Point_Dst[0][1]= "Cac";

Line_array[1][0] ="BD";Point_Src[1][0] = "Bbd"; Point_Dst[1][0]= "Dbd";

Line_array[k][ ] stores direted lines which start from the Box indicated by Src_array[k]. Point_Src[k][ ] stores all joint points on the source box (Src_array[k]) and Point_Dst[k][ ] is for joint points on the destination box (Dst_array[k]).

Now you may understand why I use a two dimension array: because I want to find the right lines and points for a given source box, by using the idex k in the above example.

Until now, I could represent the picture (Not to draw the picture) like this:

Directed lines Line_array[k][0] and Line_array[k][1] start from Point_Src[k][0] and Point_Src[k][1] respectively. Both points are on box Src_array[k], and these two lines point to Point_Dst[k][0] on box Dst_array[k][0] and Point_Dst[k][1] and Dst_array[k][1] respectively.

Then I need to get these values or names stored in these arrays from outside of this method. That is my problem. Maybe it is a wrong way, but as a newbie, I didn't find out other solutions.

Thank you very much for any hints!

Linzia at 2007-7-14 20:41:08 > top of Java-index,Java Essentials,Java Programming...
# 5

I still don't see the need for a 2-d array.

You're defining a graph.You're referring to the nodes as "boxes" but the standard term is "node".

Create a Node object to represent nodes.

Now you want vertices between nodes. ("vertex" or "edge" is also a standard term; you've used the word "joint".) You can do this a couple ways. One way is to have a java.util.Collection of other Node objects within a given Node object. Each Node within the collection represents an edge.Alternatively, you can create an Edge class, each instance of which contains a Node for the source and a Node for the target. A Node can contain a collection of Edges.

Once you have your graph set up, you can either identify a single Node from which all other nodes can be reached, and just use that as a handle onto your graph; or you could create a Collection of all node objects.

Basically, I'm saying that the first dimension of your array can be the overall list of nodes, but the second dimension is data that really ought to be folded into each object.

paulcwa at 2007-7-14 20:41:08 > top of Java-index,Java Essentials,Java Programming...