Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gmail.opozitus;
- import java.util.Scanner;
- import java.util.Random;
- public class ArrayToString {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner sc = new Scanner(System.in);
- Random rnd = new Random();
- System.out.println("Введіть розмір масиву");
- int n = sc.nextInt();
- sc.close();
- int[] a = new int[n];
- for (int i = 0; i < a.length; i++) {
- a[i] = rnd.nextInt(10);
- }
- // System.out.println(Arrays.toString(a));
- System.out.println(toString(a));
- }
- static String toString(int[] a) {
- StringBuffer sb = new StringBuffer("[]");
- int index = 0;
- for (int temp : a) {
- index++;
- sb.insert(index, temp);
- }
- index = 2;
- for (int i = 0; i < a.length - 1; i++) {
- sb.insert(index, ", ");
- index += 3;
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement