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
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:
- how to Create a Java Package from MATLAB Code?Best solution by Stack Overflow
- How to convert a char array into an int in C++?Best solution by Stack Overflow
- How to load a Java web app in the terminal?Best solution by stackoverflow.com
- How to hashMap an array in Java?Best solution by Stack Overflow
- How to create a java applet?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.