Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate int[ ] array from console on 1 line with Lambda (java)

try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
     Object[] test = Arrays.stream(br.readLine().split(" ")).map(string -> Integer.parseInt(string)).toArray();
     System.out.println(Arrays.toString(test));
} catch (IOException E) {

}

So this code works but it returns an array of type Object[] However, what I want is to make it return an array of type int[].

does anyone have an idea of how I can accomplish that?

like image 717
Все Едно Avatar asked May 18 '17 16:05

Все Едно


People also ask

How do I print an array of elements in one line?

Print Array in One Line with Java StreamsArrays. toString() and Arrays. toDeepString() just print the contents in a fixed manner and were added as convenience methods that remove the need for you to create a manual for loop.

Can you use lambda expression array?

An array is a fixed size element of the same type. The lambda expressions can also be used to initialize arrays in Java.

How do you assign an array to an int in Java?

To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15}; Or, you could generate a stream of values and assign it back to the array: int[] intArray = IntStream.

How to populate an array in Java?

Use the for Loop to Populate an Array in Java Use the Arrays.copyOf () Method to Fill Element in a Java Array Use the Arrays.fill () Method to Fill Elements in a Java Array Based on the user definition, the array will be primitive, or the object (or non-primitive) references of a class.

How to define an int array in Java?

Answer: There are several ways to define an int array in Java; let’s take a look at a few examples. 1) Declare a Java int array with initial size; populate it later If you know the desired size of your array, and you’ll be adding elements to your array some time later in your code, you can define a Java int array using this syntax:

How can I populate the 11 values generated from the for loop?

How can I populate the 11 values generated from the above for loop into my array? First you need to define what numbers is, you have only declared it. Then insert the values you want.

How do I create an int array with initial elements?

Depending on your needs you can also create an int array with initial elements like this: // (1) define your java int array int [] intArray = new int [] {4,5,6,7,8}; // (2) print the java int array for (int i=0; i<intArray.length; i++) { System.out.println (intArray [i]); }


1 Answers

to retrieve an array of int type rather than Object, you can use the mapToInt method.

int[] test = Arrays.stream(br.readLine().split(" "))
                   .mapToInt(Integer::parseInt).toArray();

note that you can simplify your code by using a method reference within the argument of the mapToInt method.

reading:

  • MKyong Java 8 Tutorials (my favorite)
  • tutorialspoint Java 8 Tutorials
  • ConcretePage Java 8 tutorials
like image 146
Ousmane D. Avatar answered Sep 19 '22 17:09

Ousmane D.