Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

arrays

 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
}
}
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;

}


}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you just havn't print it!
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot. In this example, if i want to return an empty array, How would I do it?
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this example, if i want to return an empty array, How would I do it?
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic