0

How to create an array that extends itself. I don't want to use the classes like ArrayList and Vector etc to do this. Instead i need to generate an array that extends it's size upon adding elements to it. This is question by my teacher.

Say for example, i want an int[] which extends it's size.

For instance, the user want to enter the student IDs into an array. The array has no fixed size since there are no fixed no. of students in this case. When the user says he wants one more, the array's size should be incremented by one.

Any answer is appreciated.

6
  • not using a container class is part of the question of your teacher? this might be helpful Commented Jul 17, 2013 at 11:00
  • What you describe is not possible: (i) either your teacher does not know what he is talking about or (ii) you have misunderstood what he is asking for. For example, he might be asking that you created a class with an array inside and create a new (larger) array when new items are added. Commented Jul 17, 2013 at 11:01
  • Why don't you want to use ArrayList? That's exactly what collections are made for. By discarding those tools, you're bound to re-invent the wheel (usually badly). Commented Jul 17, 2013 at 11:02
  • @JoachimSauer This is not my question in fact. I dont understand why she has asked such a question that too to me! ;) Commented Jul 17, 2013 at 11:03
  • 1
    @SayOff: then go back to your teacher. The only thing that I could think of that would make sense is that your teacher pretty much asks you to re-implement ArrayList as an exercise. That would make sense, but keep in mind that that's for learning only and there's usually no other reason to do that. Commented Jul 17, 2013 at 11:04

5 Answers 5

3

Arrays are fixed in length, you can not increase or decrease the size of array.

What you can do create new array with larger size and copy the values using Arrays#copyOf source array to new destination array.

Note: Arrays#copyOf internally call System.copy which does shallow copy.

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

5 Comments

I already said that answer to my teacher, she says that this is just a common answer! She in turn asks what if i want more than the limit?
arrays are fixed in length, you can not increase it
internally ArrayList or vector does the same
To put it differently: you can produce something that behaves similarly to an array and has the requested features (that thing will end up looking very similar to an ArrayList, however), but you can't do what you're being asked with a real array.
@MarounMaroun She might rather scold me, i actually messed up with these for a 4 days and if she knows that i asked here, she might decrease my marks! She might not know about SO! ;)
2

Here is a useful link for your teacher, from the docs:

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

The only option to do that without ArrayList/Vector.. is creating a new array and copying the values to it.

Comments

1

Your description, 'When the user says he wants one more, the array's size should be incremented by one.' is just a pointer array, which is LinkedList in java.

Comments

0

Whatever your teacher says there is no way to resize a array dynamically without creating a new array with edited size. I don't think any language supports this requirement. Just create a new array and copy the existing one.

1 Comment

Well, C (at least on Unix) has realloc, so you can certainly change the size of already-requested memory.
0

I think i'll have to re-initialize the array with an incremented size but before that, i think i'll have to copy all those elements into a temporary array and then again copy them into the original array whose size is changed.

If this is correct, my teacher might be looking for this. But that degrades the performance though.

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.