About 1,140,000 results
Open links in new tab
  1. java - Any way to declare an array in-line? - Stack Overflow

    Feb 28, 2016 · As Draemon says, the closest that Java comes to inline arrays is new String[]{"blah", "hey", "yo"} however there is a neat trick that allows you to do something like array("blah", "hey", "yo") with the type automatically inferred.

  2. Java - How to Print an Array in One Line - Stack Abuse

    Nov 23, 2021 · In this short tutorial, we'll take a look at how to print an array in a single line in Java, using Arrays.toString(), Arrays.deepToString(), the Stream API and other methods.

  3. How To Print An Array In Java In One Line - Know Program

    How To Print An Array In Java In One Line | In this post, we will discuss how to print an array in Java in one line. While displaying an array we can use a print() method instead of println() method inside the loop so that it can display all array elements in the same line.

  4. How to initialize an array in one line in Java - Sentry

    May 15, 2023 · If you want to use an ArrayList collection instead of an array, you cannot use the {...} initialization syntax. Instead, you can achieve the same in one line using Arrays.asList, as in the following example:

  5. Any way to declare an array in-line? - matheusmello.io

    Java provides a compact way to declare an array in-line while making the method call. Here's how you can achieve it: By using the new keyword and specifying the array type within square brackets [], you can directly create and pass the array to the method in one line! 🚀. To help solidify your understanding, let's walk through an example:

  6. Java List Initialization in One Line - Baeldung

    Apr 4, 2025 · We can create a List from an array. And thanks to array literals, we can initialize them in one line: List<String> list = Arrays.asList(new String[]{"foo", "bar"}); We can trust the varargs mechanism to handle the array creation. With that, …

  7. Initialize array with values in one line - dev cases

    Mar 21, 2019 · In this tutorial we will check how we can initialize list with values in one line. Please be aware, that such method of initialization will create a list backing array (see array-backed-list for details). It’s good when the list will be read only or there won’t be modification of list’s length.

  8. Java: How initialize an array in Java in one line?

    Jul 1, 2010 · The first line is working, but second line is not working. How can I make the initialization from the second line in one single line of code?

  9. How to Initialize an Array in Java in One Line?

    In Java, you can initialize an array in a single line using array literals. However, you must ensure the syntax is correct and that the number of elements is defined properly.

  10. How to initialize a list in a single line in Java with a specified ...

    Dec 16, 2019 · Given a value N, the task is to create a List having this value N in a single line in Java. Examples: Input: N = 5 Output: [5] Input: N = GeeksForGeeks Output: [GeeksForGeeks] Approach: Get the value N; Create an array with this value N; Create a List with this array as an argument in the constructor; Below is the implementation of the above ...

Refresh