• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays

 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • 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;
}
}
 
gaurav abbi
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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
  • 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;
}
}
}
 
Raghav K Aggarwala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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;

}


}
 
fujun cao
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you just havn't print it!
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
In this example, if i want to return an empty array, How would I do it?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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.
 
Paul Clapham
Sheriff
Posts: 21566
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic