Changing size of array?
-
I'm doing a project for my java class and I need to change the size of an array. I know you can't actually change the size but I've heard you can do it some other way. Basically I'm writing a class for a list of int's and one of the methods I'm working on returns an array of the first few elements in the original array. The parameter is the number of elements to return. I've figured that out but the second part has me stuck. The method also has to take the original array and shorten it so that the elements returned are removed. I know you can make a shorter new array and transfer the data from the original to the new. But I need to make it so that after this method is called, any reference to the original array would be referring to the shorter one. Anyone know how i can do this?
-
Answer:
Addressing your last sentence. You can easily have the identifier point to a different array. Here's the skeleton of what you want: public int[] shortArr( int[] longArr ) { ...int shorter = // some length to be determined ...int[] retArr = new int[shorter]; // logic to fill the retArr ...return retArr; } Then in your invoking method int[] longArr = { 1,7,8,24,44 100, 72,...}; // whatever that array is longArr = shortArr( longArr ); and now longArr points to the array returned by your method. +add A comment about Ratchetr's post. You have a choice. If you want the original name to point to the new, shorter array then do what I wrote in that last statement. If you want to keep the old array then use int[] shorterArr = shortArr( longArr ); Your choice. ++add I interpret: "But I need to make it so that after this method is called, any reference to the original array would be referring to the shorter one." to mean that the identifier would now point to the newer, shorter array. That is what would happen with my last statement, repeated here: longArr = shortArr( longArr ); In a way, this is similar to what the String method substring CAN do: String str = new String( "a big ole string" ); str = str.substring( 0, 5 ); now str is "a big" The original string is gone, the identifier points to a different string. You can also say: str = "a different animal"; and now the same identifer points to still another string. Object identifiers can be reassigned, ie, point to a different object. In retrospect I see that I shouldn't have given that method a name so similar to the arrays that's it's involved with!! +++add Now I see what you're getting at with 'ANY'. I don't think that is what's meant. I think that what is meant is that of the same identifer pointing to a shorter array. +iv add Wow, there's a method in the Arrays class to do exactly what you want: int[] shortArr = Arrays.copyOf( original, newLength ); This will produce an array that only have the first newLength elements of the original array. http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#copyOf(int%5B%5D, int)
Kyle at Yahoo! Answers Visit the source
Other answers
It is better to use containers such as Vector, ArrayList, etc which take care of their sizes automatically. Why to re-invent the wheel
James Bond
Are you *required* to use arrays in your solution? Because arrays are the wrong solution. You want to be using a collection class, like ArrayList (or Vector). You can't resize an existing array in Java. You can, of course, allocate a new array and copy elements from the old array to it. But, yup...you have a problem there. Callers still have a reference to the old array, and its size hasn't changed. With a collection class like ArrayList, you CAN remove elements from the array, and the original reference to that array will reflect the changes. If you absolutely *must* use an Array (sometimes homework assignments force you to do things the wrong way), then callers can NOT keep a reference to the original array. They will need to call some method on some class to get the array. It's doable, it's just adding a level of indirection to the problem, but it seems too messy and more complex than what your HW is probably asking you to do. ETA: @Modulo. I think you underestimated this: "But I need to make it so that after this method is called, any reference to the original array would be referring to the shorter one." Key word being ANY. int[] longArr = { 1,7,8,24,44 100, 72,...}; // whatever that array is int[] someRef = longArr; // Someone has a ref to the original array longArr = shortArr( longArr ); if( someRef.length != longArr.length) System.out.println("OOPS"); Or at least that's how I read what the OP is trying to do. Maybe I'm making it too complicated?
Ratchetr
Related Q & A:
- How to post an array of complex objects (that has an array of complex objects) with jQuery?Best solution by Stack Overflow
- how can i remove an array from an array?Best solution by Stack Overflow
- Changing reversing light on a focus?Best solution by Yahoo! Answers
- Why do I have trouble with my display picture not changing?Best solution by in.answers.yahoo.com
- What snowboard waist size is best for size 11.5 boots?Best solution by Yahoo! Answers
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.