0

How can I add an element to an array while expanding the length of the array by one unit without using ArrayList? I'm a student, and it's forbidden for the assignment.

I have written a method that does it (sort of). It increments the array length every time its called but it only saves the last input parameter tot the very last element (every other element is made null when I create the new array with new length). The method is called in my constructor that makes an instance of an object from every line scanned in a text file, and I am supposed to add this line to an instance array.

3
  • 3
    Show us the code you've written. Commented Feb 10, 2016 at 3:25
  • 1
    Can you "cheat" by reading how Java does it? Commented Feb 10, 2016 at 3:28
  • Try to write some code... Then show them to us. We will help later on. :) Commented Feb 10, 2016 at 9:10

1 Answer 1

1

You can't.

You can only create a new array which is longer than the old one. Other references of your program, already having a reference to the old array, are not affected and keep the old sized array.

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

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.