Generic Method is not working - java

I'm trying to use this method but I get an error in Eclipse saying the type argument is incorrect and it tells me to change the method signature. Any reason why?
/**Creates an independent copy(clone) of the T array.
* #param array The 2D array to be cloned.
* #return An independent 'deep' structure clone of the array.
*/
public static <T> T[][] clone2DArray(T[][] array) {
int rows=array.length ;
//clone the 'shallow' structure of array
T[][] newArray = array.clone();
//clone the 'deep' structure of array
for(int row=0;row<rows;row++){
newArray[row]=array[row].clone();
}
return newArray;
}

The copy2DArray method you posted appears to work as advertised. Perhaps you are calling the method incorrectly? Also make sure you are not using primitive types instead of objects in the array you are copying. In other words, use Integer instead of int.
Here is an example of the working method:
public class Main {
// ...
// Your copy2DArray method goes here
// ...
public static void main(String[] args) {
// The array to copy
Integer array[][] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8}
};
// Create a copy of the array
Integer copy[][] = clone2DArray(array);
// Print the copy of the array
for (int i = 0; i < copy.length; i++) {
for (int j = 0; j < copy[i].length; j++) {
System.out.print(copy[i][j] + " ");
}
System.out.println();
}
}
}
This code will print:
0 1 2
3 4 5
6 7 8

It works with all classes. It doesn't work with primitives (int, long, etc.)
So instead of using primitive arrays use the wrapper classes: use Integer[][] instead of int[][].
You can use commons-lang ArrayUtils.toPrimitive(..) and ArrayUtils.toObject(..) to convert arrays between primitives and their wrappers.

You may be using primitive types, when there's no erasure for primitive types. i.e., int.class is completely impossible, but Integer.class works. Wherever generics are involved, primitive types are a no-go.

Related

What does it mean to return a reference to an array?

I was doing some exercises on arrays, and I was prompted to return a reference to an array after copying it element by element. What does this exactly mean?
My code is the following:
public static int[] cloneArray(int array[])
{
int[] arraycopy = new int[array.length];
for(int i = 0; i < array.length; i++)
{
arraycopy[i] = array[i];
}
return arraycopy;
}
I don't know what I should be returning though as a "reference": should I return an array of ints or an int? Whenever I try to print the array, I get a weird combination of characters and numbers (unless I invoke Arrays.toString()).
"Return a reference to an array" just means "return an array".
Java only returns values, which are either primitives or object references (ie for objects, the value is a reference).
Although Java is based on C, it doesn't sully itself with pointers etc like C does.
In Java, arrays and objects do not act like primitive types such as int. Consider the following code:
public class MyClass {
public static int method1(int ar[]) {
int x = ar[1];
ar[1] = 3;
return x;
}
}
Now suppose that somewhere else, the follow code is executed:
int abcd[] = new int[3];
abcd[0] = 0;
abcd[1] = 1;
abcd[2] = 2;
int d = MyClass.method1(abcd);
System.out.println(abcd[1]);
What would be printed? It's not 1, but 3. This is because the method was not given the data in the array, it was told the location of the array. In other words, it was passed a reference. Because it was using a reference, changing the value of an array index changed its value in the code that called it. This would not have happened if method1 had taken an int as an argument.
Basically, in Java, methods do not accept arrays as arguments or return arrays. They only use references to arrays. The same goes for objects (except for Strings, which are passed by value).
In Java, Objects are only accessed by reference. Just return the Array object.

Varargs method modifies caller's array instead of its own copy?

I have this simple varargs method which divides each item in the list:
import java.util.*;
class A {
static long f(long... xs) {
Arrays.sort(xs);
long y = 100000000;
for (int i = xs.length - 1; i >= 0; i--)
y /= xs[i];
return y;
}
static {
System.out.println(f(5,2,6,3,9,3,13,4,5));
long[] xs = new long[]{5,2,6,3,9,3,13,4,5};
System.out.println(Arrays.toString(xs));
System.out.println(f(xs));
System.out.println(Arrays.toString(xs));
}
}
I'd expect it to pass a copy of the array, but apparently it's somehow modifying the array I pass in, instead of its own local copy:
$ javac A.java && java A
79
[5, 2, 6, 3, 9, 3, 13, 4, 5]
79
[2, 3, 3, 4, 5, 5, 6, 9, 13]
So I wrote this simple test program:
class B {
static void f(Object... os) {
System.out.println(os);
}
static {
Object os = new Object[]{1,2,3};
System.out.println(os);
f(os);
}
}
And it does what I expect, it clones the object array before passing it into f (hence different object identifiers):
$ javac B.java && java B
[Ljava.lang.Object;#1242719c
[Ljava.lang.Object;#4830c221
So how then is f in A modifying the caller's array instead of its own copy?
It looks like you've tricked yourself here:
Object os = new Object[]{1,2,3};
System.out.println(os);
f(os);
Since os is typed as Object, it gets interpreted as the first element of the varargs array. What gets passed into the method is actually a new Object[] whose single element is your Object[].
If you do the following, it will print the same instance:
Object[] os = new Object[]{1,2,3};
System.out.println(os);
f(os);
The f method will need make a defensive copy of the array itself in order to guarantee that an array passed in by the caller isn't modified. As arshajii points out, varargs are foremost array parameters, with the "bonus" behavior of creating a new array when given an argument list.
Anyway you can use Arrays.copyOf to make the copy, which delegates to (the less type-safe) System.arraycopy.
varargs is an array type, but with syntactic sugar to allow an on-the-fly array creation if elements are passed separately as parameters.
That is, these two signatures are identical:
static long f(long... xs) {
static long f(long[] xs) {
Except that the varargs may be called with separate elements instead of an array
Of course the array will get modified if you bypass the on-the-fly creation and create an array yourself to pass in.
So how then is f in A modifying the caller's array instead of its own copy?
It doesn't have its own copy. It has a reference to the caller's array.
In the end, an array is an object, so you don't modify the array reference itself instead its contents which is allowed.

Reading and setting byte[] in function

I need to pass a byte[] in a Java method, and inside the method i need to "change" the byte[], but i don`t want to change the local byte[], i want to change the passed byte[] to the method, like a pointer, for example.
byte[] arr = new byte[10];
doSomethingWithByteArr(arr);
// And now, arr would have changed...
Java is pass-by-reference-value when passing non-primitive arguments to methods, and is pass-by-value when passing primitives, meaning that if you have a byte array and you pass it to a modification method, that object will be modified.
Example:
public static void main(String[] args) {
byte[] arr = new byte[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
// arr now contains { 0, 1, 2, 3, ... 9 }
System.out.println(Arrays.toString(arr));
randomize(arr);
// arr now contains (ex.) { 6, 3, 7, 1, 9, 3, 2, 6, 3, 0 }
System.out.println(Arrays.toString(arr));
}
public void randomize(byte[] arr) {
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
arr[i] = random.nextInt(10);
}
}
Not sure if i understand your dilemma.
Java passes its parameters by value every time. This meaning if its a primitive it passes the value of it, and if its an object or array, it passes the value of the object referenced by it.
So if you pass an array you can modify it and since its an alias it will be reflected on the array outside of the method. But you wont be able to point the arr variable to a new reference from within the method code, you could return a new byte[] and assigning to it if you want to change it.
To my knowledge, Java doesn't support passing parameters by reference.
In Java only primitive types are copied when passed to a function. That pseudocode should work as you expect.
Although in Java everything is passed by copy, non primitive types are actually references, that's why your byte[] refers to the same instance inside and outside the function.

Java: Generic Static Multidimensional Arrays

If it is possible, how can I create a static multidimensional array in Java with different primitive datatypes per dimension?
By static, I mean the primitive array that is not dynamic like an ArrayList would be.
You can't.
A multidimensional array is, by definition, an array of arrays of arrays .... of something. So there's no way for any of those dimensions except the last to be anything other than an array. At least, by the traditional definition anyway. But if you mean something else by "multidimensional array", you'll need to tell us what that is.
As for "static", that is a heavily overloaded word in programming and every language that I can think of uses it to mean something slightly different. In Java, static means "belongs to a class, rather than to instances of that class." Again, if you mean something else by "static" here, you'll need to tell us what that is.
Edit: As originally posted, the question didn't include the word "primitive". That changes it a bit. Indeed, I agree that it would be nice from a convenience standpoint if Java allowed arrays to be indexed by char or even an enum rather than just int. But it doesn't.
Dimensions in an Array are always from type int. Think about it!
int a = 4;
int b = 5;
Shoe shoe = new Shoe (Color.RED, 42, "Leather");
Hat hat = new Hat (17, Color.Black);
Foo foo = foos[a][b];
Zilch pop = bars[shoe][hat]; // no go
If you have a multidimensional array of Foos, the first dimension is an Foo, the second an array of Foos, the third an array of array of Foo. The only variable type is the one at the bottom.
Edit after update of question:
Arrays aren't called static or primitive. Their size is fixed on initialization, and what they have in common with primitives is, that they are a buildin, which is threated special in some cases. They are - in contrast to the so called primitive types, which aren't that primitive (they have, for example, operators, exclusively for their own, like * / -) but meanwhile they are objects, but not declared in the library.
Call them build in-types.
Using Bhesh Gurung's trick:
Object[] arr = {new Integer[]{}, new String[]{}, new Double[]{}};
is begging for trouble, and it is not made of different datatypes per dimension. Let's start with the dimensions:
// One-dimensional object:
JPanel [] panels = new JPanel [3];
// Two-dimensional object:
JPanel [][] panels = new JPanel [3][10];
You have JPanels on the bottom level, and an Array of JPanel on the next dimension. You can add more dimension, and will always get an additional (Array of ...) wrapped around.
You can not mix different datatypes in an Array like int and char, or JPanel and JFrame, or int and JButton. Only if you abstract over the difference, and use an JComponent for JPanel and JFrame as common parent, but this will not work for the build-in types int, char, boolean and so on, because they aren't objects.
But can't you use autoboxing, and use Integer instead of int, Character instead of char, and then use Object as common parent class? Yes, you could, but then you're not using the primitives any more, and you're begging for troubles.
Dan is talking about a different thing - using differnt types for indexing in the multi-dimensional array:
byte b = 120;
short s = 1000;
String o [][] = new String[b][s];
b = 7;
s = 9;
o[b][s] = "foobar";
String foo = o[b][s];
There is no problem using bytes or shorts, but you can't restrict the size of an Array by declaring it as byte or short. In most cases the boundaries of a build-in integer type will not fit to a datatype (think 365 days per year), especially, since all types might get negative, so bounds-checking is necessary although and can't be restricted to compile time.
But now to the trouble:
We could declare the array as two-dimensional from the beginning:
Object[][] ar2 = {
new Integer [] {4, 5, 6},
new String [] {"me", "and", "you"},
new Character [] {'x', 'y', 'z'}};
That works fine, and makes the inner arrays accessible immediately without casting. But it is only known for the compiler, that the elements are Object arrays - the underlying type is abstracted away, and so we can write:
ar2[1][1] = 17; // expected: String
ar2[2][0] = "double you"; // expected: Char
This will compile flawlessly, but you're shooting yourself in the foot and get a Runtime exception for free.
Here is the source as a whole:
public class ArrOfMixedArr
{
public static void main (String args[])
{
Object[] arr = {
new Integer [] {1, 2, 3},
new String [] {"you", "and", "me"},
new Character [] {'a', 'b', 'c'}};
show (arr);
byte b = 7;
short s = 9;
String o [][] = new String[200][1000];
o[b][s] = "foobar";
String foo = o[b][s];
Object[][] ar2 = {
new Integer [] {4, 5, 6},
new String [] {"me", "and", "you"},
new Character [] {'x', 'y', 'z'}};
show (ar2);
// exeptions:
ar2[1][1] = 17; // expected: String
ar2[2][0] = "double you"; // expected: Char
}
public static void show (Object[] arr)
{
for (Object o : arr)
{
if (o instanceof Object[])
show ((Object[]) o);
else
System.out.print (o.toString () + "\t");
}
System.out.println ();
}
}
Now what is the solution?
If your base-types arrays of (int, byte, char, String, JPanel, ...) are of equal length, then you have something like a hidden Object, a database-row. Use a class instead:
class Shoe {
byte size;
String manufactor;
java.math.BigDecimal price;
java.awt.Color color;
}
Shoe [] shoes = new Shoe [7];
If you don't have different types of the same size, they might be unrelated, and should not be put in a common container.
After some testing, I have a simple solution:
Object [][] array = new Object [10][2];
array[0][0] = 2;
array[0][1] = false;
Well, you could define an array of an array of ... an array of Objects (nested with as many levels as dimensions) and at the bottom level fill each array with a different type ... and then, when you need to extract a value, cast it to the appropriate type. Too much work for what is worth, really. Java is no good for this kind of things, being a statically-typed language.
Maybe you should reconsider, why would you need such a data structure.
You can get the effect by using an object array:
final static Object tryit[][] = {
{'a',4},
{'b',7},
{'c',8},
};
#Test
public void accessArray( ) {
for (int i = 0; i < tryit.length ; i++) {
char letter = (Character)tryit[i][0];
int value = (Integer)tryit[i][1];
System.out.println(letter + " has value " + value);
}
}
The "#Test" is JUnit annnotation.
Note this approach would be subject to NullPointer and ClassCast exceptions at runtime if the wrong data is entered in the array.

Writing `double...` instead of `[] double`

public MonomialPolynomial(double... coeffs){
List<IMonom> monoms = new ArrayList<IMonom>();
for (int i = 0; i < coeffs.length; i++) {
// zero coeffs might yaffect the degree
if(coeffs[i] != 0)
monoms.add(new Monom(coeffs[i], i));
}
super.monoms = monoms;
}
Why do people write double... and not [] double when they mean to array?
Is there a special meaning for this?
double... declares it a "var args" parameter -- inside your method it's identical to double[] but for the caller, it's much easier to call with varying numbers of arguments (hence var args) without the need to explicitly create an array:
Without var args:
MonomialPolynomial(double[] coeffs) { ...}
...
// explicit array creation necessary
new MonomialPolynomial(new double[] {1, 2, 3)};
With var args:
MonomialPolynomial(double... coeffs) { ...}
...
// array gets created implicitly, so less boilerplate code
new MonomialPolynomial(1, 2, 3);
Edit: One thing to watch out for with var args, only the last argument of a method can be a var args argument. This guarantess there's no ambiguity when calling a method, e.g.
foo(int a, int b...) is unambigious, because the first argument will always be assigned to a and anything after that will be go into of b.
foo(int a..., int... b) on the other hand is ambigious, there's no way to tell if foo(1, 2, 3) means a={1} and b={2, 3} or a={1, 2} and b={3}.
double... is a varargs (variable arguments) feature. This means you can provide the constructor with an array of doubles, or you can provide the constructor with any number of doubles, and it is put into an array for you. (Example: MonomialPolynomial(1.0, 5.0, 6.0).)
It is the vararg feature, that is, all the arguments the function gets are collected into an array. In that case, an array of doubles. The feature is for hardcoded arrays and such. If MonomialPolynomial were to accept a double[], you would have to use such oneliners:
MonomialPolynomial(new double[] {1, 2, 3});
But with variable arguments you may write a bit neater code:
MonomialPolynomial(1, 2, 3);

Categories