简体   繁体   中英

Java general array Questions

I need to put some different size machines in the storage

I am thinking so I have an array of machines with a size of 30 present storge Machine[] machine = new Machine[30];

Machine has a size in the constructor. eg, Machine m1 = new Machine(10);

Every machine have different size, some machine may need size 5, some may need size 10. if I insert a size 5 machine into array, it should take the first 5 slots in the array. if I put a machine with size 10 at the third position, it should take slots from 3-13

public class JavaApplication23 {

/**
 * @param args the command line arguments
 */
static Machine[] machine= new Machine[30]; 


public void findSlot(Machine unit)
{
    int slot = 0;
    for(int i = 0; i < machine.length; i++)
    {
        if(fit(i,unit)==true)
        {
            System.out.println("slot "+i+" is empty");
        }
        else
        {
            System.out.println("The line is full");
        }
    }

}

public boolean fit(int num, Machine machine)
{
    boolean check = true;
    if(machine[num]==null)
    {
        for(int i = 0; i < machine.getWidth(); i++)
        {
             if(machine[num+i]!=null)
             {
                 check = false;
             }
        }       
    }
    if(check == false)
    {
        System.out.println("machine does not fit");
    }
    return check;

}
    }

The first function is looking for the empty slot where machine can fit. For example, if machine has a size of 10. slots 1-5 is taken, 8-10 is taken. I can't use 6-7 because is too small. so the function should tell me put in position 11, because 11-20 is empty

the second function is helping to check if the machine can fit


Here is my question. since my array is an array of machine. No matter what size of machine i put in, it will only take one slot. I need an an array with a total size of machines, which one size equal to one slot. so i can put machine into storage one by one and fit.

您可以添加一个类GroupOfMachines,一个int和一组机器,然后遍历该类并添加每台机器

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