getting floats rather than objects out of an Arraylist - java

I am trying to create a method that removes duplicates from a 2d array. the outside array conains point indexes and the inner array contains their coordinates. It looks like i have to use an arraylist in order to remove elements without ending up with null values in the array. I would then like to convert the arraylist back into a 2D array in order to return it in the format i require. The problem is that the arraylsit contains an array of objects so i can't cast it into an array designed for floats. what is the correct syntax for filtering the floats from my array list. my code follows:
public class rem_duplicates {
public float [][] rem_geo_duplicates(float a[][]){
ArrayList<float[]> al = new ArrayList<float[]>();
float no_points = a.length;
int count = 0;
for (int i = 0; i < no_points-1; i++){
if((a[i][0] == a[i+1][0])&&(a[i][1] == a[i+1][1])){
a[i] = null;
count ++;
}
for (int j = 0; j < no_points; j++){
if (a[j] != null){
al.add(a[j]);
}
}
//how do i get the arraylist 'al' into this array b[][]?
float b[][] = new float [a.length-count][3];
b = al.toArray();
}
}
return b
}

Try using the following:
float b[][] = new float [a.length-count][3];
b = al.toArray(b);
This is the generic version of toArray() which in your case will return a float[][]. Keep in mind that float[] is an object, so there are no issues of boxing/unboxing here.
I notice several basic issues with your code however - I recommend trying to compile it and resolving the errors.

It works fine if you pass your newly created array as parameter:
float b[][] = new float[a.length-count][];
b = al.toArray(b);

Related

Multiplying an array and a 2-d array in java

I'm trying to multiply an array and a 2d array on java and my program compiles but keeps returning the error java.lang.NullPointerException; null when I try to input anything into it. Here is my code so far:
static double[][] productWithDiagonal(double[] a, double[][] b)
{
double[][] c = new double[3][];
{
for (int i = 0; i < b.length; ++i) {
for (int j = 0; j < b[1].length; ++j) {
c[i][j] = a[j] * b[i][j];
}
}
}
return c;
}
Thanks
This here:
double[][] c = new double[3][];
Only instantiates your "rows". You need something like
double[][] c = new double[3][3];
Or more useful probably
... c = new double[b.length][b[0].length];
instead. But just to be sure: those numbers there matter; you should make sure that b for example is really a "regular rectangle" shaped matrix - so that all rows have the same number of columns. And of course a should have the same number of columns as b, too. You could add such checks in the beginning of your method; to ensure that the shapes of a and b actually allow for this multiplication!
You see, in Java, a two-dim array is nothing but an array that contains another array. Your initial code would only initiate that "outer" array, leaving the "inner" arrays at null.

Implementing Selection Sort: Inputted array is changed along with returned array

I have a program that manipulates Cartesian points. At one spot in the program, I load my x and y values into a Point array. No big deal. But then, I want to sort this array by x values and set it to a new variable while still keeping the original array the way it is.
The original array is "points" and the new array is defined as:
points1 = sortBy(points, "x");
The sortBy method is defined earlier as:
private static Point[] sortBy( Point[] pointsXY, String xOrY) {
int min;
Point[] inputPoints = pointsXY;
if( xOrY == "x"){
for( int i = 0; i < (inputPoints.length - 1); i++ ) {
min = i;
for( int j = i+1; j < inputPoints.length; j++ ) {
if( inputPoints[j].getX() < inputPoints[min].getX()) min = j;
}
// Swap points[i] and points[min]
Point temp = inputPoints[i];
inputPoints[i] = inputPoints[min];
inputPoints[min] = temp;
}
There is an "else if" block later that takes "y" instead. It then does: return inputPoints;
However, what I am getting when I am printing these arrays out are the exact same thing - two arrays sorted by x values. I have tested the output by just outputting the unsorted array, and it works fine.
I also have tried using this method without it being static, which has given the same output.
Point[] inputPoints = pointsXY; does not copy the array, it just creates a new name (inputPoints) for the same array. To create a copy of the array, you should use Point[] inputPoints = pointsXY.clone(); (javadoc).

Java method to multiply an ArrayList of decimal values by an array of doubles to get an array of ints?

Help! I have this school assignment that wants me to write a method to find the area of rectangles (an array of ints) by multiplying width (an ArrayList) by length (an array of doubles). I'm very very new to coding; I've tried for over five hours to get this working, but I keep doing things wrong and I simply can't get it right. This is the code for the method that I've written:
public void calcRectangleArea(int index, ArrayList width, double[] length, int[] area)
{
double temp = length[index];
for(index = 0; index < length.length; index++)
{
for(index = 0; index < width.size(); index++)
{
Object widthObj = (int)width.get(index);
area[index] = temp * widthObj;
}
}
}
The full starter code we were given is here, if you need more context (it's commented): http://pastie.org/pastes/916496
Thank you so much for any help you can give me in writing this method. I've been working for hours and I just can't get it...
The length of the array and size of the arraylist should be same , And you have to change the method logic a bit, Have a look at the below code snippet
public static int[] calcRectangleArea(List<Double> width, double[] length)
{
int[] area=new int[length.length];
for(int index = 0; index < length.length; index++)
{
area[index] = (int) (length[index]*width.get(index));
}
return area;
}
Call this method passing width arraylist and length array. It will return area array of ints
You dont really need two loops here. Assuming that width[1] correlates to length[1] you can just loop through both collections at the same time in the same loop.
This should work (i haven't written a line of java in ~2 years so it may not be 100%)
public void calcRectangleArea(int index, ArrayList width, double[] length, int[] area)
{
//assuming length.length == width.size
for(index = 0; index < length.length; index++)
{
int anArea = (int)(width.get(index) * length[index]);
area[index]=anArea;
}
}
again the code above assumes the size of the collections are the same.
Firstly, you don't need to assign temporary variables:
double temp = length[index];
...
Object widthObj = (int)width.get(index);
Since you will only reference them once. Reference them directly instead:
area[index] = length[index] * (int)width.get(index);
Secondly, your for loops are unneeded, and they are declared wrong. You're trying to increment the index (and twice nonetheless) that was passed to the function which will cause problems. If you were to use nested for loops, you would declare a new iterator variable for each of them:
for (int i = 0; i < something; i++) {
for (int j = 0; j < somethingElse; j++) {
doSomething();
}
}
However in this case you don't even need them.
In addition, you should not have created an Object when you wanted to cast an int:
Object widthObj = (int)width.get(index);
should have been
int width = (int)width.get(index);
However again, this line is unnecessary, and you shouldn't be casting to int this early.
Ultimately, all you need to do is the one line:
area[index] = (int)(length[index] * width.get(index));

arraylist of a three-dimensional integer vector

I have this three-dimensional array named bands. I need to do 4 copies of it, so I can work in parallel with all of them.
int bands[][][] = new int[param][][];
I need the array to keep being a three dimensional array, as it is the input for some methods that needs an int [][][]
How could I do such copies? I was thinking about using an arrayList like this:
List<Integer[][][]> bandsList = new ArrayList<Integer[][][]>();
bandsList.add(bands);
but I get this error on the last line: The method add(Integer[][][]) in the type List<Integer[][][]> is not applicable for the arguments (int[][][])
so what should I do??
The errors is because int[][][] is not the same as Integer[][][].
int[][][] is an 3D array of primitive int.
Integer[][][] is an 3D array of object Integer, which is the wrapper class of int.
Well, technically a 3D array is an array of pointers to a 2D array, which is an array of pointers to a 1D array which is an array of primitives or pointers to objects.
Use List<int[][][]> bandsList = new ArrayList<int[][][]>(); instead.
Also note that
bandsList.add(bands);
bandsList.add(bands);
will simply add 2 pointers to the same array, changing one will also change the other.
You'll need to manually copy them:
int[][][] getCopy(int[][][] bands)
{
int[][][] newBands = new int[bands.length][][];
for (int i = 0; i < bands.length; i++)
{
newBands[i] = new int[bands[i].length];
for (int j = 0; j < bands[i].length; j++)
{
newBands[i][j] = new int[bands[i][j].length];
System.arraycopy(bands, 0, newBands, 0, bands[i][j].length))
}
}
return newBands;
}
// to add
bandsList.add(getCopy(bands));
in this way you aren't doing a copy of the array, you have to do this 4 times:
int cloneList[][][] = new int[param][..][..];
for(int i = 0; i<bands.length; i++) {
int arr1[][] = bands[i];
for(int j = 0; j<arr1.length; j++) {
int arr2[] = arr1[j];
for(int k = 0; k<arr2.length; k++) {
int x = arr2[k];
cloneList[i][j][k] = x;
}
}
}

add values in a array list

I have the following ArrayList
ArrayList<double[]> db_results = new ArrayList<double[]>();
Which is populated by the following loop
double[] nums = new double[3];
for ( int i = 0 ; i <= 2; i++) {
double val = Double.parseDouble(i);
nums[i] = val;
}
db_results.add(nums);
How can i add the values from the same position in each array to make another array??
So 1+1+1=3 would be position one of the new array 2+2+2=6 would be position two of the new array and 3+3+3=9 would be position three of the new array??
Cheers
A nested loop would do it.
I would encourage you to take the time to do a Java tutorial or read a textbook. This is really basic stuff, and you'd do better learning the language properly than learning by trial and error interspersed with random SO questions.
By the way, this line from your code won't compile:
double val = Double.parseDouble(i);
The i variable is declared as an int and the parseXxx methods take a String argument. To convert an int to double, just assign it:
double val = i;
This might be what you're looking for:
double[] newArray = new double[3];
for (double[] array : db_results) {
for (int i = 0; i < 3; ++i) {
newArray[i] += array[i];
}
}
It will work after db_results has been populated. You can also compute the sum array at the same time that db_results is being populated using slukian's method.
Either a java math function or a nested loop its for your answer. Try yourself its just a mathematical calculation.

Categories