473,402 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Returning array

Hello! I am new in java.Can any body tell me how to return an array and to access it also......
Is it
return a; // a[] is an array from method static How
// and to access
int c[] = How();
Sep 10 '07 #1
5 2338
JosAH
11,448 Expert 8TB
Hello! I am new in java.Can any body tell me how to return an array and to access it also......
Is it
return a; // a[] is an array from method static How
// and to access
int c[] = How();
Yep, that's all there is to it: return an array (it's just an object) and use it as
an ordinary array, just like other arrays.

kind regards,

Jos
Sep 10 '07 #2
Thx ,
but then why error is occuring in following code:
:::::::::::::::::::::::::
class D
{public static void main(String as[])
{ String S ="Hello";
char a[] = Breakit(S);
for(int i =0;i<S.length();i++)
System.out.println(a[i]+"-");
}

static char Breakit(String S)
{char a[] = new char[S.length()];
for(int i =0;i<S.length();i++)
a[i] = S.charAt(i);
return a;
}

}
:::::::::::::
Plz help.
Sep 10 '07 #3
r035198x
13,262 8TB
Thx ,
but then why error is occuring in following code:
:::::::::::::::::::::::::
class D
{public static void main(String as[])
{ String S ="Hello";
char a[] = Breakit(S);
for(int i =0;i<S.length();i++)
System.out.println(a[i]+"-");
}


static char Breakit(String S)
{char a[] = new char[S.length()];
for(int i =0;i<S.length();i++)
a[i] = S.charAt(i);
return a;
}

}
:::::::::::::
Plz help.
1.) Always use code tags when posting code.
2.) Your Breakit method is specified as returning a char not a char[].
Expand|Select|Wrap|Line Numbers
  1.   static char[] Breakit(String S) ...
is probably what you wanted.

Edit: You should also indent your code properly and make sure your braces, {}, match and are at the right positions.
Sep 10 '07 #4
Hi,
Complete noob to java, sorry for adding to this thread..
It seems to me that I can return a 1D array if I call a func in scriptcontrol for lang java, or an item of 2D array, but apparently not the 2D array, that I can see... eg back to vbscript.
Is that expected?
Best, randall
Expand|Select|Wrap|Line Numbers
  1. function SortArray(arrArray) {
  2. var list = [
  3. ["1","359","No a Test xxxxxxxxxxxxxxx","0","08/18/2003","Approved"],
  4. ["2","268","test character fields for 500","129","07/14/2003","Approved"],
  5. ["3","288","textarea filled XXXXXXXx","0","07/15/2003","Approved"]];
  6.      list.sort(value);
  7.     return list[2][1];
  8.   }
  9.   function value(a,b) {
  10.        a = a[3]+a[1];
  11.        b = b[3]+b[1];
  12.   return a == b ? 0 : (a < b ? -1 : 1);
  13.         }
So "return list[2][1];" works,
but not "return list;"?
Sep 12 '07 #5
r035198x
13,262 8TB
Hi,
Complete noob to java, sorry for adding to this thread..
It seems to me that I can return a 1D array if I call a func in scriptcontrol for lang java, or an item of 2D array, but apparently not the 2D array, that I can see... eg back to vbscript.
Is that expected?
Best, randall
Expand|Select|Wrap|Line Numbers
  1. function SortArray(arrArray) {
  2. var list = [
  3. ["1","359","No a Test xxxxxxxxxxxxxxx","0","08/18/2003","Approved"],
  4. ["2","268","test character fields for 500","129","07/14/2003","Approved"],
  5. ["3","288","textarea filled XXXXXXXx","0","07/15/2003","Approved"]];
  6.      list.sort(value);
  7.     return list[2][1];
  8.   }
  9.   function value(a,b) {
  10.        a = a[3]+a[1];
  11.        b = b[3]+b[1];
  12.   return a == b ? 0 : (a < b ? -1 : 1);
  13.         }
So "return list[2][1];" works,
but not "return list;"?
We can only help with the Java part.
return list[2][1] may or may not work depending on the return type of the method and the type of the array itself. Perhaps you can post the Java code you're having problems with so we can understand your problem better.
Sep 12 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Krackers | last post by:
How do you write a function which returns a reference to an array. I can only get a function to return a copy of the array itself. I've had a look at some other threads in this group an the return...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.