0

i am trying to have an array of classes with 10 memebers

package localpackage;

import localpackage.Class;
public class School {
    int code;
    String name;
    Class[] classes = new Class[10];
    public School(int _code, String _name, Class _classes[]){
        code = _code;
        name = _name;
        classes = _classes;
    }
}

javi3y
  • 13
  • 2
  • i want my constructor to only accept a list of 10 – javi3y Nov 29 '21 at 14:45
  • Just a hint that java naming conventions recommend not using "_" before variables https://stackoverflow.com/questions/20677249/why-we-add-underscore-before-variable-name – Kujoen Nov 29 '21 at 14:45
  • @javi3y again: what is your question? if you don't want the other parameters, delete them. If you want the array to be exactly sized ten, throw an exception if it has any other size – Stultuske Nov 29 '21 at 14:47
  • this is my first time writing java code sorry i didn't know i shouldn't do it – javi3y Nov 29 '21 at 14:47
  • So if I understand correctly you want to ensure in the constructor that the array classes[] has a length of 10 ? – Kujoen Nov 29 '21 at 14:48
  • @Stultuske ok thanks but how can i do it – javi3y Nov 29 '21 at 14:48
  • @Kujoen exactly – javi3y Nov 29 '21 at 14:49
  • @javi3y I gave a detailed description of what to do for both cases. At this point you aren't even clear what it is you are trying to achieve. Try and implement it yourself first – Stultuske Nov 29 '21 at 14:49

2 Answers2

1

It seems you want to ensure a minimum size of a variable in the constructor. Take a look here, you will find what you need:

set minimum size of a variable array in the constructor

To name some of the solutions here, you can either directly check it with something like:

    if (name.length() < 12){
        throw new IllegalArgumentException("name must have at least 12 characters");
    }

Or you can move the check to a seperate Method and call it from the constructor.

Finally, you can use annotations like:

class Kingdom {
    @MinLength(12)
    private String name;

    ...
}

But then you need to create the Annotation class and implement the behaviour yourself.

Kujoen
  • 173
  • 1
  • 8
0

You can either use ArrayList and define your 10 items :

     List<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);
        arr.add(5);
        arr.add(6);
        arr.add(7);
        arr.add(8);
        arr.add(9);
        arr.add(10);

Or from array :

         ArrayList<String> gfg = new ArrayList<String>(
            Arrays.asList(1,2,3,4,5,6,7,8,9,10));

Or use a nCopy :

List<Integer> list = new ArrayList<Integer>(Collections.nCopies(10, new ItemsToCreate());

Or you can use a for loop

How can I initialize an ArrayList with all zeroes in Java?

Unknow
  • 178
  • 2
  • 2
  • 12
  • This will probably be usefull for him to ensure he can pass arrays of a min length, however, he was asking about how to enforce variables passed to the constructor have a minimum length. – Kujoen Nov 29 '21 at 14:53
  • Oh, I didnt saw that. Hope it will help anyway. – Unknow Nov 29 '21 at 14:56