Multiple return arrays --algorithm

one silly way is join them together.

but each of array's size is dynamic.

I hope find a way to implement it.

please see my pusedo code

publicvoid foo()// or public int[] foo()

{

int a[] =new a[500];

while(true){

...

while(case 1true){

a = ....// form array a, its size is over time.

// hope return a here for other methods

}

}

thanks

[957 byte] By [ardmorea] at [2007-11-27 8:55:17]
# 1
If your data's size is dynamic, use collections: http://java.sun.com/docs/books/tutorial/collections/index.html
BigDaddyLoveHandlesa at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 2
the usual single point of exit?Can we have Multiple return statements to provent previous return values are override.
ardmorea at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 3

I'm not sure I understand your question.

Do you mean "Can you have multiple return statements?".

Then, yes!public int[] foo(Object arg) {

if (arg == null) {

return new int[1] {0};

}

return new int[3] {1, 2, 3};

}

Or, "Can I have multiple return values?".

Well, yes, sortof.

// Either

public void foo(int[] out1; int[] out2) {

out1[0] = 1;

out2[1] = 2;

}

//...

int[] a = new int[1];

int[] b = new int[1];

foo(a, b);

// now a[0] == 1 and b[0] == 2

// or more commonly

class Bar {

int[] first = new int[1];

int[] second = new int[1];

}

//...

public Bar foo() {

Bar result = new Bar();

result.first[0] = 1;

result.second[0] = 2;

}

//...

Bar bar = foo();

// now bar.first[0] == 1 and bar.second[0] == 2

dwga at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 4
> Can we have Multiple return statementsYes, but only one return statement's value will actually be returned.
jverda at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 5
> > Can we have Multiple return statements> > Yes, but only one return statement's value will> actually be returned.and it's the first one encountered by your program as it flows through your method.
petes1234a at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 6

> > > Can we have Multiple return statements

> >

> > Yes, but only one return statement's value will

> > actually be returned.

>

> and it's the first one encountered by your program as

> it flows through your method.

Not necessarily:

try {

return 1; // 1st return

}

finally {

return 2; // 2nd return

}

The above will return 2 (unless wrapped in another try statement whose finally returns something else).

Note that this is a horrible way to write code. You should never return, throw, break, or continue out of finally. I'm just pedantically, ballsniffingly countering the claim that it's the first return statement whose value is returned. So sue me.

:-)

jverda at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 7
> Not necessarily:D鈓n! Busted! Thanks for correcting me.
petes1234a at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 8

nice reply, I really appreciate all of guys.

I still have a problem. PLease see my code.

int i = 0;

...

while(line=br.readLine() !=null)//parse a file

{

i++;

if(line=="hello")

{

...

while((line=br.readLine()) != null && ..)

{

i++;

int x = ..//assign x a value

}

return x;

}

}

I found that the cpu executes into infinity. When I track the i value,I found after return x then come back, i becomes 0 again, and String line begins from the first row of the file again.

Is the problem that after return x, it goes out of the method.Once it comes back, variables are refreshed by the memory?

sorry for my english.

ardmorea at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 9
DO NOT use == when comparing Strings or any other object. Use the equals method.
floundera at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 10

yes, i used equals.

soory the question is return an array.

public int[] xxx() {

int i = 0;

...

while(line=br.readLine() !=null)//parse a file

{

i++;

if(line=="hello")

{

...

while((line=br.readLine()) != null && ..)

{

i++;

int x[] = ..//assign x array values by a for loop

}

return x;

}

}

}

I guess it is a return array to arise such a problem, how to treat it?

ardmorea at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 11

This

while((line=br.readLine()) != null && ..)

{

i++;

int x = ..//assign x a value

}

return x;

Is emitting a foul and unpleasant odor of which the only possible source is that you have an class variable of some description that is named x.

cotton.ma at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 12
> yes, i used equals.> soory the question is return an array.This code has the same problem as I just described.
cotton.ma at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 13
> yes, i used equals.Not in the code you posted. If you do actually use it then it might be a damn good idea posting your actual code and not some half arsed crap and making us guess what the code is supposed to do.
floundera at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 14

ok,cotton.please my whole code.

God bless you.

import java.io.*;

import java.util.List;

import java.util.ArrayList;

import java.util.LinkedList;

import java.util.ListIterator;

import java.util.regex.*;

import java.util.StringTokenizer;

public class ParseFile

{

private String Filename="test.txt";

private double x1, y1, x2, y2; // to plot lines from (x1,y1) to (x2,y2)

private double xy[]= new double[206];

public double[] xyforStroke()

{

// parse a file to read line by line;

File file = new File(Filename);

try

{

BufferedReader br = new

BufferedReader(new FileReader(Filename));

String line;

String s1 = "0.5 setlinewidth 0.0 setgray";

String s2 = "stroke clear";

String[] parts;

int iter = 0;

while ((line = br.readLine()) != null )

{

iter++;

System.out.printf("iterative at %d: ",iter);

line = line.trim();

System.out.println(line);

if (line.equals(s1)) //true

{

List<Double> xList = new ArrayList<Double>();

List<Double> yList = new ArrayList<Double>();

System.out.printf("iterative at %d: ",iter);

line = line.trim();

System.out.println(line);

while ((line = br.readLine()) != null && !line.equals(s2))

{

iter++;

System.out.printf("iterative at %d: ",iter);

line = line.trim();

System.out.println(line);

parts = line.split("\\s+");

xList.add(Double.parseDouble(parts[0]));

yList.add(Double.parseDouble(parts[1]));

}

Double[] x = xList.toArray(new Double[xList.size()]);

Double[] y = yList.toArray(new Double[yList.size()]);

System.out.println("Test in ParseFile.java");

for (int i = 0; i<xList.size();i++)

{

xy[i]= x[i].doubleValue();

xy[i+xList.size()] = y[i].doubleValue();

System.out.printf("%10.5f\n",xy[i]);

}

xy[xy.length-1] = xList.size();

// put non-zero x or y elements's size at the last element of array xy;

//setxy(xy); // pass array reference

System.out.printf("return first time when iter=%d\n",iter);

return xy;

}

//return xy;

}

}

catch (Exception exception) {

exception.printStackTrace();

}

System.out.printf("return null here");

return null;

}

>

ardmorea at 2007-7-12 21:16:09 > top of Java-index,Java Essentials,New To Java...
# 15
It's probably not a good idea to return a reference to the same object over and over again if you're expecting different return values each time.I would move private double xy[]= new double[206]; into the method body instead of declaring it in the class.
myncknma at 2007-7-21 22:50:02 > top of Java-index,Java Essentials,New To Java...
# 16
I moved it into the body, still wrong.I really expect different return values each time.
ardmorea at 2007-7-21 22:50:02 > top of Java-index,Java Essentials,New To Java...
# 17

the questions is I am going parse a txt file which has many same data structure sections.

I try to parse each section over time and return them for plotting.

However I only can get the first section return values then repeat forever.

The data file is

%!PS-Adobe-3.0 EPSF 3.0

%%BoundingBox: 0 0 612 792

%%Title: Developmental Testing

%%CreationDate: 06/07/2007 08:45:57

%%EndComments

/Helvetica findfont 9 scalefont setfont

0.5 setlinewidth 0.0 setgray

20.000020.0000 moveto

592.000020.0000 lineto /

592.0000 772.0000 lineto /

20.0000 772.0000 lineto /

20.000020.0000 lineto /

stroke clear

22.000022.0000 moveto

590.000022.0000 lineto /

590.0000 770.0000 lineto /

22.0000 770.0000 lineto /

22.000022.0000 lineto /

stroke clear

...

If I remove the return statement, I found that I can go through the entire data file, but how can I return each section data?

ardmorea at 2007-7-21 22:50:02 > top of Java-index,Java Essentials,New To Java...