SHARE
TWEET

Untitled

a guest Jul 19th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int[] intArray = new int[] {1, 2, 3, 4, 5};
  2. System.out.println(intArray);     // prints something like '[I@3343c8b3'
  3.      
  4. // Array of primitives:
  5. int[] intArray = new int[] {1, 2, 3, 4, 5};
  6. //output: [1, 2, 3, 4, 5]
  7.  
  8. // Array of object references:
  9. String[] strArray = new String[] {"John", "Mary", "Bob"};
  10. //output: [John, Mary, Bob]
  11.      
  12. String[] array = new String[] {"John", "Mary", "Bob"};
  13. System.out.println(Arrays.toString(array));
  14.      
  15. [John, Mary, Bob]
  16.      
  17. String[][] deepArray = new String[][] {{"John", "Mary"}, {"Alice", "Bob"}};
  18. System.out.println(Arrays.toString(deepArray));
  19. //output: [[Ljava.lang.String;@106d69c, [Ljava.lang.String;@52e922]
  20. System.out.println(Arrays.deepToString(deepArray));
  21.      
  22. [[John, Mary], [Alice, Bob]]
  23.      
  24. double[] doubleArray = { 7.0, 9.0, 5.0, 1.0, 3.0 };
  25. System.out.println(Arrays.toString(doubleArray));
  26.      
  27. [7.0, 9.0, 5.0, 1.0, 3.0 ]
  28.      
  29. int[] intArray = { 7, 9, 5, 1, 3 };
  30. System.out.println(Arrays.toString(intArray));
  31.      
  32. [7, 9, 5, 1, 3 ]
  33.      
  34. System.out.println(Arrays.toString(array));
  35.      
  36. System.out.println(Arrays.deepToString(array));
  37.      
  38. import java.util.Arrays;
  39. .
  40. .
  41. .
  42. System.out.println( Arrays.toString( myarray ) );
  43.      
  44. String[] strArray = new String[] {"John", "Mary", "Bob"};
  45.  
  46. // #1
  47. Arrays.asList(strArray).stream().forEach(s -> System.out.println(s));
  48.  
  49. // #2
  50. Stream.of(strArray).forEach(System.out::println);
  51.  
  52. // #3
  53. Arrays.stream(strArray).forEach(System.out::println);
  54.  
  55. /* output:
  56. John
  57. Mary
  58. Bob
  59. */
  60.      
  61. System.out.println(Arrays.asList(array));
  62.      
  63. String[] greeting = {"Hey", "there", "amigo!"};
  64. String delimiter = " ";
  65. String.join(delimiter, greeting)
  66.      
  67. int[] ints = new int[] {1, 2, 3, 4, 5};
  68. System.out.println(IntStream.of(ints).mapToObj(Integer::toString).collect(Collectors.joining(", ")));
  69. System.out.println(IntStream.of(ints).boxed().map(Object::toString).collect(Collectors.joining(", ")));
  70. System.out.println(Arrays.toString(ints));
  71.  
  72. String[] strs = new String[] {"John", "Mary", "Bob"};
  73. System.out.println(Stream.of(strs).collect(Collectors.joining(", ")));
  74. System.out.println(String.join(", ", strs));
  75. System.out.println(Arrays.toString(strs));
  76.  
  77. DayOfWeek [] days = { FRIDAY, MONDAY, TUESDAY };
  78. System.out.println(Stream.of(days).map(Object::toString).collect(Collectors.joining(", ")));
  79. System.out.println(Arrays.toString(days));
  80.  
  81. // These options are not the same as each item is printed on a new line:
  82. IntStream.of(ints).forEach(System.out::println);
  83. Stream.of(strs).forEach(System.out::println);
  84. Stream.of(days).forEach(System.out::println);
  85.      
  86. public static void main(String[] args) {
  87.     int[] intArray = new int[] {1, 2, 3, 4, 5};
  88.     String[] strArray = new String[] {"John", "Mary", "Bob"};
  89.  
  90.     //Prior to Java 8
  91.     System.out.println(Arrays.toString(intArray));
  92.     System.out.println(Arrays.toString(strArray));
  93.  
  94.     // In Java 8 we have lambda expressions
  95.     Arrays.stream(intArray).forEach(System.out::println);
  96.     Arrays.stream(strArray).forEach(System.out::println);
  97. }
  98.      
  99. public static void main(String[] args) {
  100.     int[][] int2DArray = new int[][] { {11, 12}, { 21, 22}, {31, 32, 33} };
  101.     String[][] str2DArray = new String[][]{ {"John", "Bravo"} , {"Mary", "Lee"}, {"Bob", "Johnson"} };
  102.  
  103.     //Prior to Java 8
  104.     System.out.println(Arrays.deepToString(int2DArray));
  105.     System.out.println(Arrays.deepToString(str2DArray));
  106.  
  107.     // In Java 8 we have lambda expressions
  108.     Arrays.stream(int2DArray).flatMapToInt(x -> Arrays.stream(x)).forEach(System.out::println);
  109.     Arrays.stream(str2DArray).flatMap(x -> Arrays.stream(x)).forEach(System.out::println);
  110. }
  111.      
  112. int[][] table = new int[2][2];
  113.      
  114. System.out.println(Arrays.deepToString(table).replaceAll("],", "]," + System.getProperty("line.separator")));
  115.      
  116. for(int n: someArray) {
  117.     System.out.println(n+" ");
  118. }
  119.      
  120. List<String> list = new ArrayList<String>();
  121. list.add("One");
  122. list.add("Two");
  123. list.add("Three");
  124. list.add("Four");
  125. // Print the list in console
  126. System.out.println(list);
  127.      
  128. String[] array = new String[] { "One", "Two", "Three", "Four" };
  129. System.out.println(Arrays.toString(array));
  130.      
  131. String[] arr1 = new String[] { "Fifth", "Sixth" };
  132. String[] arr2 = new String[] { "Seventh", "Eight" };
  133. String[][] arrayOfArray = new String[][] { arr1, arr2 };
  134. System.out.println(arrayOfArray);
  135. System.out.println(Arrays.toString(arrayOfArray));
  136. System.out.println(Arrays.deepToString(arrayOfArray));
  137.      
  138. for (int i = 0; i < intArray.length; i++) {
  139.    System.out.print(intArray[i] + ", ");
  140. }
  141.      
  142. public String toString() {
  143.     return getClass().getName() + "@" + Integer.toHexString(hashCode());
  144. }
  145.      
  146. public String toString() {
  147.     if (this instanceof boolean[])
  148.         return Arrays.toString((boolean[]) this);
  149.     if (this instanceof byte[])
  150.         return Arrays.toString((byte[]) this);
  151.     if (this instanceof short[])
  152.         return Arrays.toString((short[]) this);
  153.     if (this instanceof char[])
  154.         return Arrays.toString((char[]) this);
  155.     if (this instanceof int[])
  156.         return Arrays.toString((int[]) this);
  157.     if (this instanceof long[])
  158.         return Arrays.toString((long[]) this);
  159.     if (this instanceof float[])
  160.         return Arrays.toString((float[]) this);
  161.     if (this instanceof double[])
  162.         return Arrays.toString((double[]) this);
  163.     if (this instanceof Object[])
  164.         return Arrays.deepToString((Object[]) this);
  165.     return getClass().getName() + "@" + Integer.toHexString(hashCode());
  166. }
  167.      
  168. System.out.println(Arrays.asList(array));
  169.      
  170. int[] a = new int[]{1,2,3,4,5};
  171.      
  172. Integer[] a = new Integer[]{1,2,3,4,5};
  173.      
  174. public void printArray(int [] a){
  175.         //write printing code
  176. }
  177.      
  178. int[] intArray = new int[] {1, 2, 3, 4, 5};
  179. Arrays.stream(intArray).forEach(System.out::println);
  180.      
  181. int[] intArray = new int[] {1, 2, 3, 4, 5};
  182. Arrays.stream(intArray).forEach(System.out::print);
  183.      
  184. int[] intArray = new int[] {1, 2, 3, 4, 5};
  185. System.out.println(Arrays.toString(intArray));
  186.      
  187. char A[] = {'a', 'b', 'c'};
  188.  
  189. System.out.println(A); // no other arguments
  190.      
  191. abc
  192.      
  193. ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
  194. System.out.println(ow.writeValueAsString(anyArray));
  195.      
  196. Gson gson = new Gson();
  197. System.out.println(gson.toJson(anyArray));
  198.      
  199. int x[] = {1,2,3};
  200.     String printableText = Arrays.toString(x).replaceAll("[\[\]]", "").replaceAll(", ", "n");
  201.     System.out.println(printableText);
  202.      
  203. 1
  204. 2
  205. 3
  206.      
  207. String[] items = {"item 1", "item 2", "item 3"};
  208.  
  209. for(int i = 0; i < items.length; i++) {
  210.  
  211.     System.out.println(items[i]);
  212.  
  213. }
  214.      
  215. item 1
  216. item 2
  217. item 3
  218.      
  219. // 1) toString()  
  220.     int[] arrayInt = new int[] {10, 20, 30, 40, 50};  
  221.     System.out.println(Arrays.toString(arrayInt));
  222.  
  223. // 2 for loop()
  224.     for (int number : arrayInt) {
  225.         System.out.println(number);
  226.     }
  227.  
  228. // 3 for each()
  229.     for(int x: arrayInt){
  230.          System.out.println(x);
  231.      }
  232.      
  233. public class printer {
  234.  
  235.     public static void main(String[] args) {
  236.         String a[] = new String[4];
  237.         Scanner sc = new Scanner(System.in);
  238.         System.out.println("enter the data");
  239.         for (int i = 0; i < 4; i++) {
  240.             a[i] = sc.nextLine();
  241.         }
  242.         System.out.println("the entered data is");
  243.         for (String i : a) {
  244.             System.out.println(i);
  245.         }
  246.       }
  247.     }
  248.      
  249. String[] strArray = new String[] { "John", "Mary", "Bob" };
  250. String arrayAsCSV = StringUtils.join(strArray, " , ");
  251. System.out.printf("[%s]", arrayAsCSV);
  252. //output: [John , Mary , Bob]
  253.      
  254. <groupId>org.apache.commons</groupId>
  255. <artifactId>commons-lang3</artifactId>
  256. <version>3.3.2</version>
  257.      
  258. int array[] = {1, 2, 3, 4, 5};
  259. for (int i:array)
  260.     System.out.println(i);
  261.      
  262. String s = new String(bytes, StandardChars.ISO_8559);
  263. System.out.println(s);
  264. // to reverse
  265. byte[] bytes2 = s.getBytes(StandardChars.ISO_8559);
  266.      
  267. String s = new String(bytes, StandardChars.UTF_8);
  268. System.out.println(s);
  269. // to reverse
  270. byte[] bytes2 = s.getBytes(StandardChars.UTF_8);
  271.      
  272. String s = DatatypeConverter.printHexBinary(bytes);
  273. System.out.println(s);
  274. // to reverse
  275. byte[] bytes2 = DatatypeConverter.parseHexBinary(s);
  276.      
  277. String s = DatatypeConverter.printBase64Binary(bytes);
  278. System.out.println(s);
  279. // to reverse
  280. byte[] bytes2 = DatatypeConverter.parseBase64Binary(s);
  281.      
  282. String s = Arrays.toString(bytes);
  283. System.out.println(s);
  284. // to reverse
  285. String[] split = s.substring(1, s.length() - 1).split(", ");
  286. byte[] bytes2 = new byte[split.length];
  287. for (int i = 0; i < bytes2.length; i++)
  288.     bytes2[i] = Byte.parseByte(split[i]);
  289.      
  290. String s = Arrays.toString(
  291.                IntStream.range(0, bytes.length).map(i -> bytes[i] & 0xFF).toArray());
  292. System.out.println(s);
  293. // to reverse
  294. String[] split = s.substring(1, s.length() - 1).split(", ");
  295. byte[] bytes2 = new byte[split.length];
  296. for (int i = 0; i < bytes2.length; i++)
  297.     bytes2[i] = (byte) Integer.parseInt(split[i]); // might need a range check.
  298.      
  299. // array of primitives:
  300. int[] intArray = new int[] {1, 2, 3, 4, 5};
  301.  
  302. System.out.println(Arrays.toString(intArray));
  303.  
  304. output: [1, 2, 3, 4, 5]
  305.      
  306. // array of object references:
  307. String[] strArray = new String[] {"John", "Mary", "Bob"};
  308.  
  309. System.out.println(Arrays.toString(strArray));
  310.  
  311. output: [John, Mary, Bob]
  312.      
  313. Arrays.stream(myArray).forEach(System.out::println);
  314.      
  315. class demo{
  316.       public static void main(String a[]){
  317.  
  318.            int[] number={1,2,3,4,5};
  319.  
  320.            System.out.print(number);
  321.       }
  322. }
  323.      
  324. class demo{
  325.       public static void main(String a[]){
  326.  
  327.            int[] number={1,2,3,4,5};
  328.  
  329.            int i;
  330.  
  331.            for(i=0;i<number.length;i++){
  332.                  System.out.print(number[i]+"  ");
  333.            }
  334.       }
  335. }
  336.      
  337. import java.util.Arrays;
  338.  
  339.  class demo{
  340.       public static void main(String a[]){
  341.  
  342.            int[][] number2={{1,2},{3,4},{5,6}};`
  343.  
  344.            System.out.print(Arrays.deepToString(number2));
  345.       }
  346. }
  347.      
  348. public static void print(int[] array) {
  349.     StringJoiner joiner = new StringJoiner(",", "[", "]");
  350.     Arrays.stream(array).forEach(element -> joiner.add(element + ""));
  351.     System.out.println(joiner.toString());
  352. }
  353.  
  354.  
  355. int[] array = new int[]{7, 3, 5, 1, 3};
  356. print(array);
  357.      
  358. [7,3,5,1,3]
  359.      
  360. int[] intArray = new int[] {1, 2, 3, 4, 5};
  361. String[] strArray = new String[] {"John", "Mary", "Bob"};
  362. ArrayUtils.toString(intArray);
  363. ArrayUtils.toString(strArray);
  364.      
  365. {1,2,3,4,5}
  366. {John,Mary,Bob}
  367.      
  368. jshell> String[] names = {"ram","shyam"};
  369. names ==> String[2] { "ram", "shyam" }
  370.  
  371. jshell> Arrays.toString(names);
  372. $2 ==> "[ram, shyam]"
  373.  
  374. jshell>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top