Skip to content
Advertisement

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 added, the array largens

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. Unfortunately, the array definition in Java

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1000000 at problem2.main(problem2.java:17)

I get the following error and I don’t know why. I tried looking it up, but I didn’t find a solution. Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1000000 at problem2.main(problem2.java:17) Here is my code: Answer The problem is not the size of the int[]. Your while loop is constantly checking if a[i] is less than 4000000 while the i variable is

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 this. Is

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(9758): obtainBuffer() track 0x30efa0

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 into a

difference between natural ordering and total ordering

I happen to come across many statements like comparable is used when natural ordering is required while sorting an array or collection and comparator for total ordering. The version you may have heard could be same or different with the same meaning but ultimately its one of the distinguishing factors between the two(comparator and comparable interfaces). But, I couldn’t find

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

Advertisement