1

I tried to print an array with random numbers from 100 to 500 by using System.out.print(); but nothing has shown and when I tried using println it wasn't organized. So, how can I organize it to print 10 elements per row by using System.out.println();

And also, when I return the array it returns the location like [I@7de26db8, how I can fix that?

public int [] randomS() {
    Random rand = new Random();
    int a[] = new int[10000];
    
    for(int i=0; i<a.length; i++)
    {
        
        a[i] = rand.nextInt(500-100)+100;
        
        System.out.print(a[i]+" ");
    }
    
    return a;
    
}
oleg.cherednik
  • 15,340
  • 4
  • 20
  • 31

5 Answers5

0
public static void main(String[] args) {
        int[] array = createRandomArray(100, 100, 500); //initialize array (size = 100, minRand = 100, maxRand = 500)
        visualizeArray(array, 10); //visualize array 10 per line
    }

    //function that creates a Random int array, and returns it
    public static int [] createRandomArray(int size, int minRand, int maxRand) {
        Random rand = new Random();
        int a[] = new int[size];
        
        for(int i = 0; i < a.length; i++){
            a[i] = rand.nextInt(maxRand - minRand) + minRand;
        }
        return a;       
    }
    
    //function to visualize an int Array with a set number of elements per line
    public static void visualizeArray (int[] array, int numbersPerLine) {
        for(int i = 0; i < array.length; i += 1) {
            if (i != 0) {
                if(i % numbersPerLine == 0)
                    System.out.println();   
            }
            System.out.printf("%d\t", array[i]);
        }
    }
AlexM28
  • 71
  • 2
0

the only change you need to make to make this program print it's output as a column is to change

System.out.print(a[i]+" "); to System.out.println(a[i]+" ");

the regular "print" method does what it says, but "println" will print the output on a new line specifically.

Additionally, when java outputs something that looks like this I@7de26db8 it is printing the memory location of the data type you are trying to return and not the data type's contents itself.

This is because you are returning the array object, so as far as java is concerned you are trying to return where that object exists within memory.

So if you wanted to return the array values which is what I assume you are trying to do you would use something along the lines of return new int[] {a1,a2,a3};

where a1, a2, and a3 are the specific array element names,

in your example you create those using a for loop so you don't know the individual names and attempting to type them all out wouldn't make much sense, so you would also include within your for loop a return line that uses [i] as the array element name

kabuto
  • 96
  • 4
0

Here is a version with an outer for 100x and an inner for 10x

import java.util.Random ;

class PrintRandom {
        public static void main (String args[]) {
        Random rand = new Random();
        for (int i = 0; i<100; i++){
                for (int j = 0; j<10;j++){
                        System.out.print(rand.nextInt(500-100)+100 +" - ");
                        }
                System.out.println("");
                }
        }
}
0

When you use System.out.print() in the for loop, you're printing all of the elements on the same line. Using System.out.println() prints each element on a new line. If you want to print 10 elements per row, you can use System.out.print() and append "\n" to every 10th value.

In the for loop below, if the remainder of i+1 divided by 10 equals 0, then "\n" is appended to the element. Otherwise, a space " " is appended. This prints 10 values per row.

public static int[] randomS() {
    Random rand = new Random();
    int a[] = new int[10000];
    
    for(int i=0; i<a.length; i++)
    {
        a[i] = rand.nextInt(500 - 100) + 100;
        System.out.print(a[i] + ((i+1)%10==0 ? "\n" : " "));
    }
    
    return a;
}

The function returns a value like [I@7de26db8 because you're returning the array object, which basically returns the identity of the object rather than its contents. To return the contents of the array, you can change the return type of the function to String and use Arrays.toString(), as shown below.

public static String randomS() {
    Random rand = new Random();
    int a[] = new int[10000];
    
    for(int i=0; i<a.length; i++)
    {
        a[i] = rand.nextInt(500 - 100) + 100;
        System.out.print(a[i] + ((i+1)%10==0 ? "\n" : " "));
    }
    
    return Arrays.toString(a);
}

If you really want an int[] array rather than a String representation of its contents, you can find out how to convert a String to an int[] array here.

ghost1034
  • 23
  • 7
0
public static void main(String... args) {
    int[] arr = createRandomArray(10_000);
    print(arr, 10);
}

private static int[] createRandomArray(int length) {
    Random random = new Random();
    int[] arr = new int[length];

    for (int i = 0; i < arr.length; i++)
        arr[i] = random.nextInt(401) + 100;

    return arr;
}

private static void print(int[] arr, int itemsPerRow) {
    for (int i = 0, j = 1; i < arr.length; i++, j++) {
        // skip space befire the first item
        if (j != 1)
            System.out.print(' ');

        System.out.print(arr[i]);

        if (j % itemsPerRow == 0) {
            System.out.println();
            j = 0;
        }
    }
}
oleg.cherednik
  • 15,340
  • 4
  • 20
  • 31