Skip to content
Advertisement

Tag: arrays

Why do arrays change in method calls? [duplicate]

This question already has answers here: Are arrays passed by value or passed by reference in Java? [duplicate] (7 answers) Is Java “pass-by-reference” or “pass-by-value”? (93 answers) Closed last month. When I write like this: It prints x22; When I write like this: It doesn’t print x22, but prints x5. Why, in the second version function, doesn’t the value change?

OutOfMemoryError on 10000×10000 int array in eclipse

I was wrote a little scratch program to check the time of looping through a 2 dimensional array by rows or columns, since I remembered that one way was much faster for large data sets, but I couldn’t remember which way. I ran into an interesting error when I ran my program though. Now in my test I am using

where is array saved in memory in java?

If I have a function that in that function I declare: Where are arr and the whole array stored? heap? stack? Does it matter if the declaration is in some function or in main()? and let’s say I also have these command lines: where are arr[0] and arr[1] stored? Answer Memory diagram: Boxes are memory locations (where binary numbers can

Java convert Arraylist to float[]

How I can do that? I have an arraylist, with float elements. (Arraylist <Float>) it is not working. cannot cast from Object[] to float[] Answer Loop over it yourself. The nullcheck is mandatory to avoid NullPointerException because a Float (an object) can be null while a float (a primitive) cannot be null at all. In case you’re on Java 8

Getting the class of the components of an array

If I have how can I get T.class from array? If I do array.getClass() that gets me T[].class instead. Answer Component Type Use this: Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null. Reference: Class.getComponentType() Safe / Unsafe casting Is there a way I can cast

Print array without brackets and commas

I’m porting a Hangman game to Android and have met a few problems. The original Java program used the console, so now I have to somehow beautify the output so that it fits my Android layout. How do I print an array without the brackets and commas? The array contains slashes and gets replaced one-by-one when the correct letter is

Comparing arrays in JUnit assertions, concise built-in way?

Is there a concise, built-in way to do equals assertions on two like-typed arrays in JUnit? By default (at least in JUnit 4) it seems to do an instance compare on the array object itself. EG, doesn’t work: Of course, I can do it manually with: ..but is there a better way? Answer Use org.junit.Assert’s method assertArrayEquals: If this method

Immutable array in Java

Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn’t actually prevent one from doing something like I want the elements of the array to be unchangeable. Answer Not with primitive arrays. You’ll need to use a List or some other data structure:

Advertisement