Skip to content

Tag: arrays

Resize an Array while keeping current elements in Java?

I have searched for a way to resize an array in Java, but I could not find ways of resizing the array while keeping the current elements. I found for example code like int[] newImage = new int[newWidth];, but this deletes the elements stored before. My code would basically do this: whenever a new element is a…

Dealing with an ArrayStoreException

throws while doesn’t. Is there any other way to deal with that exception without creating a temporary String[] array? Answer In Java an array is also an object. You can put an object of a subtype into a variable of a supertype. For example you can put a String object into an Object variable. Unfortunate…

How to calculate the median of an array?

I’m trying to calculate the total, mean and median of an array thats populated by input received by a textfield. I’ve managed to work out the total and the mean, I just can’t get the median to work. I think the array needs to be sorted before I can do this, but I’m not sure how to do t…

how to convert short array to byte array

I have found converting a short to byte array, and byte array to short array, but not short array to byte array. Here is the code leading up to the conversion I have tried and However I receive this error on both (the error if not exactly the same but very similar for both): 05-29 13:41:12.021: W/AudioTrack(9…

3-Dimension List or Map

I need something like a 3-dimension (like a list or a map), which I fill with 2 Strings and an Integer within a loop. But, unfortunately I don’t know which data structure to use and how. Answer Create an object that encapsulates the three together and add them to an array or List: If you want to insert …

Sort an array in Java

I’m trying to make a program that consists of an array of 10 integers which all has a random value, so far so good. However, now I need to sort them in order from lowest to highest value and then print it onto the screen, how would I go about doing so? (Sorry for having so much code for a

Convert a 2D array into a 1D array

Here is the code I have so far: I seem to be stuck at this point on how to get [i][s] into a single dimensional array. When I do a print(temp) all the elements of my 2D array print out one a time in order but cannot figure out how to get them into the 1D array. I am a