Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "arrays" Watch "arrays" New topic
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: 3419
    
  12
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: 18541
    
    8

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: arrays
 
Similar Threads
Problem in passing ArrayList
Cloning Doubt
toArray method of List
Issue about ArrayList
init String arrays in code blocks/methods