Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,926 questions

51,859 answers

573 users

How to fill an 2D array with different unique numbers in Java

2 Answers

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[][] array2D = new int[4][5]; 
        int n;
        
        for (int i = 0; i < array2D.length; i++ ) 
            for (int j = 0; j < array2D[i].length; j++ ) {
                 do {
                        n = (int)(Math.random()*100);
                } while (NumberExist(array2D, n));
                array2D[i][j] = n;
            }
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                System.out.print(array2D1[j] + " ");
            }
            System.out.println();
        }
    }
    static boolean NumberExist(int[][] array2D, int number) {
            
        for (int i = 0; i < array2D.length; i++ ) 
            for (int j = 0; j < array2D[i].length; j++ ) 
                 if (array2D[i][j] == number) return true;
            
        return false;  
    }
}
 
/*

run1:

59 37 30 62 23 
87 72 41 2 35 
14 5 80 18 53 
45 32 46 92 55 

run2:

98 33 24 20 4 
38 69 60 95 44 
81 50 39 41 34 
85 23 17 86 25 

run3:

94 96 23 33 81 
77 7 22 32 89 
50 97 4 93 43 
51 37 61 60 38 

*/

 



answered Oct 12, 2016 by avibootz
0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[][] array2D = new int[4][5]; 
        int n;
        
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                do {
                    n = (int)(Math.random()*100);
                } while (NumberExist(array2D, n));
                array2D1[j] = n;
            }
        }
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                System.out.print(array2D1[j] + " ");
            }
            System.out.println();
        }
    }
    static boolean NumberExist(int[][] array2D, int number) {
            
        for (int[] array2D1 : array2D) {
            for (int j = 0; j < array2D1.length; j++) {
                if (array2D1[j] == number) {
                    return true;
                }
            }
        }
            
        return false;  
    }
}
 
/*

run1:

82 6 97 65 47 
49 80 23 93 73 
4 43 41 66 38 
35 12 22 70 57 

run2:

39 91 23 34 25 
99 88 4 82 66 
36 77 27 78 87 
65 35 37 56 61 

run3:

80 1 55 52 48 
6 89 86 94 10 
27 74 56 62 66 
72 36 7 12 46

*/

 



answered Oct 12, 2016 by avibootz
...