Finding the size of a char array in Java

You can use b.length to find out how many characters there are. This is only the number of characters, not indexing, so if you iterate over it with a for loop, remember to write it like this: for(int i=0;i < b.length; i++) Note the < (not a <=). It’s also important to note that since the array isn’t a class, .length isn’t a … Read more

Getting the array length of a 2D array in Java

Consider public static void main(String[] args) { int[][] foo = new int[][] { new int[] { 1, 2, 3 }, new int[] { 1, 2, 3, 4}, }; System.out.println(foo.length); //2 System.out.println(foo[0].length); //3 System.out.println(foo[1].length); //4 } Column lengths differ per row. If you’re backing some data by a fixed size 2D array, then provide getters to … Read more

Get only part of an Array in Java?

The length of an array in Java is immutable. So, you need to copy the desired part into a new array.Use copyOfRange method from java.util.Arrays class: int[] newArray = Arrays.copyOfRange(oldArray, startIndex, endIndex); startIndex is the initial index of the range to be copied, inclusive.endIndex is the final index of the range to be copied, exclusive. (This index may lie outside the … Read more

Checking to see if array is full

Since you are using array, the size of array is determined during compilation. Thus if your intention is to check whether current array’s index has reached the last array element, you may use the following condtion (possibly in a loop) to check whether your current array index is the last element. If it is true, … Read more

How to use ArrayUtils for array of objects, it doesn’t delete the content of an array

Change this ArrayUtils.remove(listItems, i); to listItems = ArrayUtils.remove(listItems, i); As you can see in the JavaDoc, the method does not change the argument listItems, rather it returns a new array with the remaining elements. Edit You also need to change your deletion method to public static ItemTracker[] deleteItem(ItemTracker[] listItems) { //.. } So you could return the … Read more

Error array dimension missing

I keep getting array dimension missing public static Planet[] readPlanets(String filename) { allPlanets = new Planet[]; In in = new In (filename); int nplanets = in.readInt(); double radius = in.readDouble(); for (int i = 0; i < allPlanets.length; i++) { double pxxPos = in.readDouble(); double pyyPos = in.readDouble(); double pxxVel = in.readDouble(); double pyyVel = … Read more

Printing char arrays in Java

Solution is to use new String(c): System.out.println(“” + new String(c)); And the “” + is really bogus and should be removed. Below is why you get what you get. System.out is a PrintStream. println() has an overload for println(char[] x): Prints an array of characters and then terminate the line. This method behaves as though … Read more

Fill an array with random numbers [duplicate]

You need to add logic to assign random values to double[] array using randomFill method. Change public static double[] list(){ anArray = new double[10]; return anArray; } To public static double[] list() { anArray = new double[10]; for(int i=0;i<anArray.length;i++) { anArray[i] = randomFill(); } return anArray; } Then you can call methods, including list() and … Read more