简体   繁体   中英

Filling two new arrays from existing array

I already have one Array with random numbers between 0-999. I also have created two new arrays, one with correct the size to hold all numbers 0-499, and one with the correct size for numbers 500-999.

Problem is to then loop through the Array holding all numbers and copying the right numbers 0-499 and 500-999 to the new Arrays.

Anyone know the correct way to do this? Have spent many days now trying to figure this out.

What i got so far:

   public static void main(String[] args) {

    Scanner scannerObject = new Scanner(System.in);
    Random generator = new Random();

    System.out.print("How many numbers between 0 - 999?" );
    int number= scannerObject.nextInt(); 

    int [] total= new int[number]; 

    for(int index = 0; index < total.length; index++ ) 
    {   
        total[index] = generator.nextInt(1000); 
    }

    System.out.println("Here are the random numbers:");

    for(int index = 0; index < total.length; index++ )
    {   

        System.out.print(total[index]+ " ");    
    }

    int lowNumber=0;
    int largeNumber = 0;
    for(int index = 0; index < total.length; index++ )  
    {   
        if (total[index] < 500)
        {       
            lowNumber++;    
        }

        if (total[index] >= 500)
        {
            largeNumber++;
        }
    }
    System.out.println();
    System.out.println(lowNumber);
    System.out.println(largeNumber);

    int [] totalLownumber = new int [lowNumber];
    int [] totalLargeNumber = new int [largeNumber];

    for(int index = 0; index < total.length; index++ )  
    {   // TODO
    }
}

2 of the approaches you can take are as follows:

  1. You can go through the initial array, count the elements you have, and use the counter values to define the size of the arrays you need. You then go over the original array once again and copy the elements to their respective array. You can use the counter values once again (you would need to reset them first) to allow you to keep track in which array location will the current number need to go. This should be similar to what you are doing.

  2. Consider using a variable length data structure such as a List (ArrayList in Java). This would allow you to go over your original array and assign the numbers to their respective list (let's call them largeNumberList and lowNumberList ). Since these collections have a dynamic size, you would only need to traverse the array once and assign as you go along.

The latter approach is usually what is used more often, however, since it would seem that this question is related to homework, I would recommend you try both approaches and compare them.

Try this:

int lowIndex = 0;
int largeIndex = 0;
for(int index = 0; index < total.length; index++ ) {
    if (total[index] < 500) {
         totalLowNumber[lowIndex++] = total[index];
    } else {
         totalLargeNumber[largeIndex++] = total[index];
}

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