How do I add to an array from a static method?

Adding to a java array?

  • So I'm working on a java project for school and I'm a little stumped. I'll try and explain it as best as I can so you can get the situation. So I have a class called BigList which contains an an array called items. items is an array of objects of the class ofList. So the method I am stumped on is called add. The method add takes a parameter of type ofList and adds it to the end array. Now what I don't get is how to make the array bigger. I thought once it was set, an array is stuck at a certain length. Any thoughts?

  • Answer:

    You're correct. Java arrays have a fixed size; it can't be increased or decreased. You'll need to create a new array of a larger size and copy the elements of the old array into it. Or you could use an ArrayList, which exists for this very purpose, instead of an array.

Paul at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

i do c++ but java and c++ are very simaler so you can try this. Using the new keyword will dynamicly make space in memory and the syntax is as follows. System.out.println("enter number of items"); //right here get input for however you do it in Java// char items[]=new char[varible_of_choice_goes_here]; in order to do this i think it would prob be best to have a temporary varible to keep current data in here. You don't have to have user input to do this but it was easiest way to show how to do it.

antiwindows

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.