0

I want to ask if below is possible in JAVA. I think the code is self-explanatory, if not, don't hesitate to ask.

import java.util.List;
public class NewClass {
    public static void main(String[] args){
        int x = 0;
        int y = 1;
        int z = 2;
        printer([x,y,z]); //if somehow this is possible in JAVA?
    }

    private void printer(List num){
        num.stream().forEach((i) -> {
            System.out.println(i);
        });
    }
}
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Hafiz Temuri
  • 3,882
  • 6
  • 41
  • 66
  • Consider [`Arrays.asList`](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList-T...-) – bradimus Sep 28 '16 at 14:16
  • @HovercraftFullOfEels, Thanks, I did check.. and it's giving an error. Basically, I want to know if there is some way around to make it possible in JAVA. – Hafiz Temuri Sep 28 '16 at 14:19
  • @TalhaTemuri It would be helpful to update your question and describe in plain language (non-code) what it is you're trying to accomplish. If the goal is to pass an array by reference, see this answer for how you can update the method signature of `printer(...)`. http://stackoverflow.com/a/1610771/149428 – Taylor D. Edmiston Sep 28 '16 at 15:53

5 Answers5

3

You want to use varargs here:

private void print(int ... numbers)

In other words: when using "..." you can pass around arbitrary "arrays"; and the compiler does the work for you. So, that method can be used like:

print(); // empty list
print(1) // one entry
print(1, 2, n) // entries ...

Your idea of using print(List) is the wrong approach - you are creating a raw type there, something you definitely want to avoid.

So the other alternative would be:

void print(List<Integer> numbers)

to be called with

print(Arrays.asList(1,2,3));
GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

Ues Varagrs ,yes it is possible but not with the exact syntax

Varagrs will collect your all pass inputs as a single array.

To use array with stream , you can use Arrays.stream function to get the stream of an array.

import java.util.List;
public class NewClass {
    public static void main(String[] args){
        int x = 0;
        int y = 1;
        int z = 2;
        printer(x,y,z); 
    }

    private void printer(int... num){
         Arrays.stream(num).forEach(System.out::println);
      // or  Arrays.stream(num).forEach(i->System.out.println(i));
    }
}

Output :

0
1
2
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

var args are your friend

http://docs.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

private void printer(int... numbers) - This is basically an array of ints

With your code you could then do Stream.of(numbers).foreach(i -> System.out.println(i))

Ash
  • 2,562
  • 11
  • 31
0

What about?

printer(Arrays.asList(new int[]{x, y, z}));

  • I don't know who gives you negative -1, but your method works out perfectly for me. I am choosing it as a correct answer. Thank you :) – Hafiz Temuri Sep 28 '16 at 14:30
  • 1
    We don't need to create a new array, so just edit out **new int[]{** from your code. – Hafiz Temuri Sep 28 '16 at 14:35
  • @TalhaTemuri I didnt downvote this question; but just for the record you might want to check out my answer to figure that a whole lot is missing in this one. – GhostCat Sep 28 '16 at 14:51
  • 1
    @Talha Temuri: since you don’t use generics, you don’t notice the difference between the intended operation of creating a `List` of size `3` and what this code actually does, i.e. creating a `List` of size `1`. The code can be compiled without error and apparently does something as `forEach` works for all elements, but look at what the program prints. Is something like `[I@36A41F6` really what you wanted to see? Or do you actually want to see `[0, 1, 2]`? – Holger Sep 28 '16 at 15:01
0
   public static void main(String[] args) throws IOException  {

       List<Integer> intList =new ArrayList<Integer>();
       int x = 0;
       int y = 1;
       int z = 2;
       intList.add(x);
       intList.add(y);
       intList.add(z);
       printer(intList);
      }

      private static void printer(List<Integer> intList) {
        for(int count=0;count<intList.size();count++)
         System.out.println(""+intList.get(count));
      }
   }