| Author |
arrays
|
phani kon
Ranch Hand
Joined: Apr 06, 2005
Posts: 251
|
|
What is the wrong in the code? when I run this code, I am not getting any value import java.util.*; public class arraylist { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub arraylist a = new arraylist(); a.getFruits(); } String[] getFruits() { // Allocate space for the array and set all elements to null. String[] s = new String [3]; // Allocate the individual elements. // at this point s[i] is null not "" s[0] = "banana"; s[1] = "strawberry"; s[2] = "orange"; return s; } }
|
 |
gaurav abbi
Ranch Hand
Joined: Jan 05, 2007
Posts: 108
|
|
hi Lakshmi, you are returning a String[] from getFruits() method, but not assignig that value to any Object(in this case a String[]). try adding this line. String[] sa = a.getFruits(); now your sa array has the values you added in getFruits(). you can check them using a for loop or by calling Arrays.toString(sa)
|
thanks,<br />gaurav abbi
|
 |
phani kon
Ranch Hand
Joined: Apr 06, 2005
Posts: 251
|
|
I have added this line as below. How should I return that value. import java.util.*; public class arraylist { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method String[] sa = a.getFruits(); } String[] getFruits() { // Allocate space for the array and set all elements to null. String[] s = new String [3]; // Allocate the individual elements. // at this point s[i] is null not "" s[0] = "banana"; s[1] = "strawberry"; s[2] = "orange"; for(int i=0; i<3; i++) { // System.out.println(""+s[i]); return s; } } }
|
 |
Raghav K Aggarwala
Greenhorn
Joined: Jun 04, 2006
Posts: 9
|
|
Lakshmi: Try this... import java.util.*; public class MyList { private String[] s ; public static void main(String[] args) { MyList a = new MyList(); String[] sa = a.getFruits(); for(int i=0; i<3; i++) { System.out.println(""+sa[i]); } //or // You can fetch values with System.out.println(Arrays.toString(sa)); } String[] getFruits() { s = new String [3]; s[0] = "banana"; s[1] = "strawberry"; s[2] = "orange"; return s; } }
|
 |
fujun cao
Greenhorn
Joined: Dec 04, 2006
Posts: 2
|
|
|
you just havn't print it!
|
 |
phani kon
Ranch Hand
Joined: Apr 06, 2005
Posts: 251
|
|
|
Thanks a lot. In this example, if i want to return an empty array, How would I do it?
|
 |
phani kon
Ranch Hand
Joined: Apr 06, 2005
Posts: 251
|
|
|
In this example, if i want to return an empty array, How would I do it?
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Maybe I'm missing something, but if you want an empty array, don't put anything in it. At the moment, your getFruits method puts three items in your array. Just delete the lines of code that do that.
|
Joanne
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Originally posted by lakshmi manepally: In this example, if i want to return an empty array, How would I do it?
That depends on what you mean by "empty". If you want an array with zero elements, then you do this:If you want an array with three elements, but each of those three elements contains null, then do what Joanne said.
|
 |
 |
|
|
subject: arrays
|
|
|