0

I have the following code that takes integers and stores it in a array of booleans according to the position. I have a method include that allows the user to input more numbers into the array however if the number is bigger than the array size then I need to increase the size. I am aware that you can't do much with arrays once they are made just change what's in each position. Is there a quick way to make it bigger or could I use arraylists to keep the array size changing?

public class ISet {
    public int max;
    boolean[] numArray;


    ISet(int a) {
        this.size = a;
        this.numArray = new boolean[size];

    }

    public void include(int n) {
        if (n > size) {
            this.size = n;
            numArray[n]=true;

        }
        else

            numArray[n]=true;


        }
3
  • 1
    Arrays once allocated memory cannot be resized. This requires to create a new array of the desired size and copy the contents of the original array to the newly created array, probably using java.lang.System.arraycopy(...); Commented Feb 10, 2014 at 18:12
  • For what purpose is this? Instead of storing "true" at index "number", consider storing "number" in a list/set/map and if a list contains a "number" that "number" is true, if not, false. Commented Feb 10, 2014 at 18:26
  • 1
    Take a look here; docs.oracle.com/javase/tutorial/collections to see which collection suits you the best. Commented Feb 10, 2014 at 18:27

4 Answers 4

2

If you want to make it bigger yourself you can use Arrays.copyOf(numArray, newLength) which will copy your array into a new new array of the specified length adding falses at the end if the new length is longer than the original.

Otherwise you can use an ArrayList.

Sign up to request clarification or add additional context in comments.

Comments

1

You can either simply use an ArrayList, although it'll have to be ArrayList<Boolean> as opposed to boolean (the primitive data type). Probably the easiest solution.

The other option would be to create a new, larger array and copy every element from the current array to the new one when the size of the current array is exceeded.

Comments

1

You should use an ArrayList.

Normal arrays have a fixed size in Java.

2 Comments

I thought I might have to. Can I use a set of booleans or is that something from python?
A Java Set would not be appropriate in this situation as its purpose is to contain unique elements.
0

For "normal" code you should use ArrayList instead.

For application that use extreme huge arrays, the array needs much less memory, than the ArrayList, because an Object needs 4 at least 4 times (16 bytes) more memory than a primitive.

For such special situations, you can use Arrays.copyOf() which copies the content into a new bigger array. See also src cocde of ArrayList.java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.