简体   繁体   中英

I'm trying to make a java array code

i'm trying to build a code for different actions in arrays with different methods (Constructors) but i cannot seem to find a way to link them...I've made a array with 100 elements and a random variable to fill it....i'd like to know how to get my random elements from the first methods to the second one to make the comparing.... and also i'd like to not include in the 0 element in the random generator..any help?

this is my code

import java.util.*;

public class prova1{
    public int min;

    public void tabele(){
        Random r = new Random();
        int d;
        int e[]= new int[100];
        for(int i=0; i<e.length;i++){
            d=r.nextInt(100);
            e[i]=d;
            System.out.println(e[i]);
        }
    }
    public void emin(){
        Random r = new Random();
        int d;
        int e[]=new int[100];
        for(int i=0; i<e.length;i++){
            d=r.nextInt(100);
            e[i]=d;
            if(e[i]<min){
                min=e[i];
            }
        }
        System.out.println("Vlera me e vogel eshte: " +min);

    }
    public static void main(String []args){
        prova1 prova = new prova1();
        prova.tabele();
        prova.emin();   
    }
}

Return the array from tabele to a local variable and send it as parameter to emin

import java.util.*;

public class prova1 {
    public int min;

    public int[] tabele() {
        Random r = new Random();
        int d;
        int e[]= new int[100];
        for(int i=0; i<e.length;i++){
            d=r.nextInt(99) + 1;
            e[i]=d;
            System.out.println(e[i]);
        }

        return e;
    }

    public void emin(int[] e) {
        Random r = new Random();
        int d;
        for(int i=0; i<e.length;i++) {
            if(e[i]<min) {
                min=e[i];
            }
        }

        System.out.println("Vlera me e vogel eshte: " +min);
    }

    public static void main(String []args){
        prova1 prova = new prova1();
        int[] arr = prova.tabele();
        prova.emin(arr);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM