0

Instead of a single parameter, I tried passing arrays as parameter and this happens. Where am I going wrong? I am a beginner, Btw!

public class ClassArrayTest
{
    public static void main(String[] args)
    {
        fruit f = new fruit({"peach","apple","mango"});
        f.display();

    }
}



class fruit
{
    String[] a;
    
    public fruit(String[] aa)
    {
        a = aa;
    }
    
    void display()
    {
        System.out.println(a);
    }
}
  • That depends on what **exactly** you are trying to do… What is your desired output and what is the actual one? I guess they are different, aren't they? – deHaar Apr 22 '22 at 07:15
  • https://stackoverflow.com/q/409784/16034206 please refer to this link for more details on why your code doesnt have the behaviour you expect :) Arrays.toString() – experiment unit 1998X Apr 22 '22 at 07:23
  • By the way, using `List` rather than array is much simpler. `List< String > inputs = List.of( "peach" , "apple" , "mango" ) ;` and `for( String input : inputs ) { System.out.println( input ) ; }`. – Basil Bourque Apr 23 '22 at 02:23

4 Answers4

1

OK ... so I think your problem is understanding the syntax of creating initialized arrays in Java. The {"peach","apple","mango"} syntax can only be used in two contexts:

String[] fruit = {"peach", "apple", "mango"};

or

new String[]{"peach", "apple", "mango"};

You were trying to use it like this:

someMethod({"peach", "apple", "mango"});

but that is not permitted. The { ... } syntax cannot be used there. But you could write this:

someMethod(new String[]{"peach", "apple", "mango"});
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0
public class ClassArrayTest {

    public static void main(String... args) {
       fruit f = new fruit(new String []{"peach","apple","mango"}); // use new Sring[]{""...} when passing an array
        f.display();
    }

}

class fruit
{
    String[] a;
    
    public fruit(String[] aa)
    {
        a = aa;
    }
    
    void display()
    {
        for(String stringElements : a){
        System.out.println(stringElements );
        }// i also find it easy to iterate through than print an object
        
    }
}
DereckChamboko
  • 187
  • 2
  • 6
0
import java.util.Arrays;

public class ClassArrayTest {
    
public static void main(String[] args) {
            fruit f = new fruit(new String[] { "A", "B", "C" });
            f.display();
        }
    }

class fruit {

    String[] a;

    public fruit(String[] aa) {
        a = aa;
    }

    void display() {
        Arrays.stream(a).forEach(System.out::println);
    }
}
0

Is this what you want?

public class ClassArrayTest
{
    public static void main(String[] args)
    {
        String[] aa = {"peach","apple","mango"};
        fruit f = new fruit(aa);
        f.display();

    }
}


class fruit
{
    String[] a;
    
    public fruit(String[] aa)
    {
        a = aa;
    }
    
    void display()
    {
        // System.out.println(a);
        for (String f: a) {
            System.out.println(f);
        }
    }
}

And I suggest that use Fruit rather than fruit to name a class in Java.

Rong Luo
  • 21
  • 3