Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- System.out.println(intArray); // prints something like '[I@3343c8b3'
- // Array of primitives:
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- //output: [1, 2, 3, 4, 5]
- // Array of object references:
- String[] strArray = new String[] {"John", "Mary", "Bob"};
- //output: [John, Mary, Bob]
- String[] array = new String[] {"John", "Mary", "Bob"};
- System.out.println(Arrays.toString(array));
- [John, Mary, Bob]
- String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
- System.out.println(Arrays.toString(deepArray));
- //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
- System.out.println(Arrays.deepToString(deepArray));
- [[John, Mary], [Alice, Bob]]
- double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
- System.out.println(Arrays.toString(doubleArray));
- [7.0, 9.0, 5.0, 1.0, 3.0 ]
- int[] intArray = { 7, 9, 5, 1, 3 };
- System.out.println(Arrays.toString(intArray));
- [7, 9, 5, 1, 3 ]
- System.out.println(Arrays.toString(array));
- System.out.println(Arrays.deepToString(array));
- import java.util.Arrays;
- .
- .
- .
- System.out.println( Arrays.toString( myarray ) );
- String[] strArray = new String[] {"John", "Mary", "Bob"};
- // #1
- Arrays.asList(strArray).stream().forEach(s -> System.out.println(s));
- // #2
- Stream.of(strArray).forEach(System.out::println);
- // #3
- Arrays.stream(strArray).forEach(System.out::println);
- /* output:
- John
- Mary
- Bob
- */
- System.out.println(Arrays.asList(array));
- String[] greeting = {"Hey", "there", "amigo!"};
- String delimiter = " ";
- String.join(delimiter, greeting)
- int[] ints = new int[] {1, 2, 3, 4, 5};
- System.out.println(IntStream.of(ints).mapToObj(Integer::toString).collect(Collectors.joining(", ")));
- System.out.println(IntStream.of(ints).boxed().map(Object::toString).collect(Collectors.joining(", ")));
- System.out.println(Arrays.toString(ints));
- String[] strs = new String[] {"John", "Mary", "Bob"};
- System.out.println(Stream.of(strs).collect(Collectors.joining(", ")));
- System.out.println(String.join(", ", strs));
- System.out.println(Arrays.toString(strs));
- DayOfWeek [] days = { FRIDAY, MONDAY, TUESDAY };
- System.out.println(Stream.of(days).map(Object::toString).collect(Collectors.joining(", ")));
- System.out.println(Arrays.toString(days));
- // These options are not the same as each item is printed on a new line:
- IntStream.of(ints).forEach(System.out::println);
- Stream.of(strs).forEach(System.out::println);
- Stream.of(days).forEach(System.out::println);
- public static void main(String[] args) {
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- String[] strArray = new String[] {"John", "Mary", "Bob"};
- //Prior to Java 8
- System.out.println(Arrays.toString(intArray));
- System.out.println(Arrays.toString(strArray));
- // In Java 8 we have lambda expressions
- Arrays.stream(intArray).forEach(System.out::println);
- Arrays.stream(strArray).forEach(System.out::println);
- }
- public static void main(String[] args) {
- int[][] int2DArray = new int[][] { {11, 12}, { 21, 22}, {31, 32, 33} };
- String[][] str2DArray = new String[][]{ {"John", "Bravo"} , {"Mary", "Lee"}, {"Bob", "Johnson"} };
- //Prior to Java 8
- System.out.println(Arrays.deepToString(int2DArray));
- System.out.println(Arrays.deepToString(str2DArray));
- // In Java 8 we have lambda expressions
- Arrays.stream(int2DArray).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);
- Arrays.stream(str2DArray).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);
- }
- int[][] table = new int[2][2];
- System.out.println(Arrays.deepToString(table).replaceAll("],", "]," + System.getProperty("line.separator")));
- for(int n: someArray) {
- System.out.println(n+" ");
- }
- List<String> list = new ArrayList<String>();
- list.add("One");
- list.add("Two");
- list.add("Three");
- list.add("Four");
- // Print the list in console
- System.out.println(list);
- String[] array = new String[] { "One", "Two", "Three", "Four" };
- System.out.println(Arrays.toString(array));
- String[] arr1 = new String[] { "Fifth", "Sixth" };
- String[] arr2 = new String[] { "Seventh", "Eight" };
- String[][] arrayOfArray = new String[][] { arr1, arr2 };
- System.out.println(arrayOfArray);
- System.out.println(Arrays.toString(arrayOfArray));
- System.out.println(Arrays.deepToString(arrayOfArray));
- for (int i = 0; i < intArray.length; i++) {
- System.out.print(intArray[i] + ", ");
- }
- public String toString() {
- return getClass().getName() + "@" + Integer.toHexString(hashCode());
- }
- public String toString() {
- if (this instanceof boolean[])
- return Arrays.toString((boolean[]) this);
- if (this instanceof byte[])
- return Arrays.toString((byte[]) this);
- if (this instanceof short[])
- return Arrays.toString((short[]) this);
- if (this instanceof char[])
- return Arrays.toString((char[]) this);
- if (this instanceof int[])
- return Arrays.toString((int[]) this);
- if (this instanceof long[])
- return Arrays.toString((long[]) this);
- if (this instanceof float[])
- return Arrays.toString((float[]) this);
- if (this instanceof double[])
- return Arrays.toString((double[]) this);
- if (this instanceof Object[])
- return Arrays.deepToString((Object[]) this);
- return getClass().getName() + "@" + Integer.toHexString(hashCode());
- }
- System.out.println(Arrays.asList(array));
- int[] a = new int[]{1,2,3,4,5};
- Integer[] a = new Integer[]{1,2,3,4,5};
- public void printArray(int [] a){
- //write printing code
- }
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- Arrays.stream(intArray).forEach(System.out::println);
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- Arrays.stream(intArray).forEach(System.out::print);
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- System.out.println(Arrays.toString(intArray));
- char A[] = {'a', 'b', 'c'};
- System.out.println(A); // no other arguments
- abc
- ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
- System.out.println(ow.writeValueAsString(anyArray));
- Gson gson = new Gson();
- System.out.println(gson.toJson(anyArray));
- int x[] = {1,2,3};
- String printableText = Arrays.toString(x).replaceAll("[\[\]]", "").replaceAll(", ", "n");
- System.out.println(printableText);
- 1
- 2
- 3
- String[] items = {"item 1", "item 2", "item 3"};
- for(int i = 0; i < items.length; i++) {
- System.out.println(items[i]);
- }
- item 1
- item 2
- item 3
- // 1) toString()
- int[] arrayInt = new int[] {10, 20, 30, 40, 50};
- System.out.println(Arrays.toString(arrayInt));
- // 2 for loop()
- for (int number : arrayInt) {
- System.out.println(number);
- }
- // 3 for each()
- for(int x: arrayInt){
- System.out.println(x);
- }
- public class printer {
- public static void main(String[] args) {
- String a[] = new String[4];
- Scanner sc = new Scanner(System.in);
- System.out.println("enter the data");
- for (int i = 0; i < 4; i++) {
- a[i] = sc.nextLine();
- }
- System.out.println("the entered data is");
- for (String i : a) {
- System.out.println(i);
- }
- }
- }
- String[] strArray = new String[] { "John", "Mary", "Bob" };
- String arrayAsCSV = StringUtils.join(strArray, " , ");
- System.out.printf("[%s]", arrayAsCSV);
- //output: [John , Mary , Bob]
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-lang3</artifactId>
- <version>3.3.2</version>
- int array[] = {1, 2, 3, 4, 5};
- for (int i:array)
- System.out.println(i);
- String s = new String(bytes, StandardChars.ISO_8559);
- System.out.println(s);
- // to reverse
- byte[] bytes2 = s.getBytes(StandardChars.ISO_8559);
- String s = new String(bytes, StandardChars.UTF_8);
- System.out.println(s);
- // to reverse
- byte[] bytes2 = s.getBytes(StandardChars.UTF_8);
- String s = DatatypeConverter.printHexBinary(bytes);
- System.out.println(s);
- // to reverse
- byte[] bytes2 = DatatypeConverter.parseHexBinary(s);
- String s = DatatypeConverter.printBase64Binary(bytes);
- System.out.println(s);
- // to reverse
- byte[] bytes2 = DatatypeConverter.parseBase64Binary(s);
- String s = Arrays.toString(bytes);
- System.out.println(s);
- // to reverse
- String[] split = s.substring(1, s.length() - 1).split(", ");
- byte[] bytes2 = new byte[split.length];
- for (int i = 0; i < bytes2.length; i++)
- bytes2[i] = Byte.parseByte(split[i]);
- String s = Arrays.toString(
- IntStream.range(0, bytes.length).map(i -> bytes[i] & 0xFF).toArray());
- System.out.println(s);
- // to reverse
- String[] split = s.substring(1, s.length() - 1).split(", ");
- byte[] bytes2 = new byte[split.length];
- for (int i = 0; i < bytes2.length; i++)
- bytes2[i] = (byte) Integer.parseInt(split[i]); // might need a range check.
- // array of primitives:
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- System.out.println(Arrays.toString(intArray));
- output: [1, 2, 3, 4, 5]
- // array of object references:
- String[] strArray = new String[] {"John", "Mary", "Bob"};
- System.out.println(Arrays.toString(strArray));
- output: [John, Mary, Bob]
- Arrays.stream(myArray).forEach(System.out::println);
- class demo{
- public static void main(String a[]){
- int[] number={1,2,3,4,5};
- System.out.print(number);
- }
- }
- class demo{
- public static void main(String a[]){
- int[] number={1,2,3,4,5};
- int i;
- for(i=0;i<number.length;i++){
- System.out.print(number[i]+" ");
- }
- }
- }
- import java.util.Arrays;
- class demo{
- public static void main(String a[]){
- int[][] number2={{1,2},{3,4},{5,6}};`
- System.out.print(Arrays.deepToString(number2));
- }
- }
- public static void print(int[] array) {
- StringJoiner joiner = new StringJoiner(",", "[", "]");
- Arrays.stream(array).forEach(element -> joiner.add(element + ""));
- System.out.println(joiner.toString());
- }
- int[] array = new int[]{7, 3, 5, 1, 3};
- print(array);
- [7,3,5,1,3]
- int[] intArray = new int[] {1, 2, 3, 4, 5};
- String[] strArray = new String[] {"John", "Mary", "Bob"};
- ArrayUtils.toString(intArray);
- ArrayUtils.toString(strArray);
- {1,2,3,4,5}
- {John,Mary,Bob}
- jshell> String[] names = {"ram","shyam"};
- names ==> String[2] { "ram", "shyam" }
- jshell> Arrays.toString(names);
- $2 ==> "[ram, shyam]"
- jshell>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement