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.
Related
We can determine the length of an ArrayList<E> using its public method size(), like
ArrayList<Integer> arr = new ArrayList(10);
int size = arr.size();
Similarly we can determine the length of an Array object using the length property
String[] str = new String[10];
int size = str.length;
Whereas the size() method of ArrayList is defined inside the ArrayList class, where is this length property of Array defined?
Arrays are special objects in java, they have a simple attribute named length which is final.
There is no "class definition" of an array (you can't find it in any .class file), they're a part of the language itself.
10.7. Array Members
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.
All the members inherited from class Object; the only method of Object that is not inherited is its clone method.
Resources:
JLS - Arrays
It's "special" basically, with its own bytecode instruction: arraylength. So this method:
public static void main(String[] args) {
int x = args.length;
}
is compiled into bytecode like this:
public static void main(java.lang.String[]);
Code:
0: aload_0
1: arraylength
2: istore_1
3: return
So it's not accessed as if it were a normal field. Indeed, if you try to get it as if it were a normal field, like this, it fails:
// Fails...
Field field = args.getClass().getField("length");
System.out.println(field.get(args));
So unfortunately, the JLS description of each array type having a public final field length is somewhat misleading :(
It's defined in the Java language specification:
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
Since there is a limitless number of array types (for every class there is a corresponding array type, and then there are multidimensional arrays), they cannot be implemented in a class file; the JVM has to do it on the fly.
Even though this is not a direct answer to the question, it is an addition to the .length vs .size() argument. I was researching something related to this question so when I came across it I noticed that the definition(s) provided here
The public final field length, which contains the number of components of the array.
is not "exactly" correct.
The field length contains the number of available places to put a component, not the number of components present in the array. So it represents the total available memory allocated to that array, not how much of that memory is filled.
Example:
static class StuffClass {
int stuff;
StuffClass(int stuff) {
this.stuff = stuff;
}
}
public static void main(String[] args) {
int[] test = new int[5];
test[0] = 2;
test[1] = 33;
System.out.println("Length of int[]:\t" + test.length);
String[] test2 = new String[5];
test2[0] = "2";
test2[1] = "33";
System.out.println("Length of String[]:\t" + test2.length);
StuffClass[] test3 = new StuffClass[5];
test3[0] = new StuffClass(2);
test3[1] = new StuffClass(33);
System.out.println("Length of StuffClass[]:\t" + test3.length);
}
Output:
Length of int[]: 5
Length of String[]: 5
Length of StuffClass[]: 5
However, the .size() property of the ArrayList does give the number of elements in the list:
ArrayList<Integer> intsList = new ArrayList<Integer>();
System.out.println("List size:\t" + intsList.size());
intsList.add(2);
System.out.println("List size:\t" + intsList.size());
intsList.add(33);
System.out.println("List size:\t" + intsList.size());
Output:
List size: 0
List size: 1
List size: 2
it's public final field , which contains the number of components of the array (length may be positive or zero)
An array thus has the same public fields and methods as the following class:
class A implements Cloneable, java.io.Serializable {
public final int length = X;
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
}
}
more info at
10.7 Array Members
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
To answer it as it-is, where is this length property of array defined? In a special Object header.
Easy to see via JOL
int [] ints = new int[23];
System.out.println(ClassLayout.parseInstance(ints).toPrintable());
One of the lines from this output is going to be:
OFFSET SIZE TYPE DESCRIPTION
16 4 (object header) 17 00 00 00 (00010111 00000000 00000000 00000000) (23)
Usually Objects have two headers (mark and klass), arrays have one more that always occupy 4 bytes in length, as size is an int.
The keyword length acts like a data filed defined. When using in an array, we can use it to access how many elements in an array. Regarding to String[], we can invoke length() method defined in String class. With regard to ArrayList, we can use size() method defined in ArrayList. Note that when creating an array list with ArrayList<>(capacity), the initial size() of this array list is zero since there is no element.
We can determine the length of an ArrayList<E> using its public method size(), like
ArrayList<Integer> arr = new ArrayList(10);
int size = arr.size();
Similarly we can determine the length of an Array object using the length property
String[] str = new String[10];
int size = str.length;
Whereas the size() method of ArrayList is defined inside the ArrayList class, where is this length property of Array defined?
Arrays are special objects in java, they have a simple attribute named length which is final.
There is no "class definition" of an array (you can't find it in any .class file), they're a part of the language itself.
10.7. Array Members
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].
A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.
All the members inherited from class Object; the only method of Object that is not inherited is its clone method.
Resources:
JLS - Arrays
It's "special" basically, with its own bytecode instruction: arraylength. So this method:
public static void main(String[] args) {
int x = args.length;
}
is compiled into bytecode like this:
public static void main(java.lang.String[]);
Code:
0: aload_0
1: arraylength
2: istore_1
3: return
So it's not accessed as if it were a normal field. Indeed, if you try to get it as if it were a normal field, like this, it fails:
// Fails...
Field field = args.getClass().getField("length");
System.out.println(field.get(args));
So unfortunately, the JLS description of each array type having a public final field length is somewhat misleading :(
It's defined in the Java language specification:
The members of an array type are all of the following:
The public final field length, which contains the number of components of the array. length may be positive or zero.
Since there is a limitless number of array types (for every class there is a corresponding array type, and then there are multidimensional arrays), they cannot be implemented in a class file; the JVM has to do it on the fly.
Even though this is not a direct answer to the question, it is an addition to the .length vs .size() argument. I was researching something related to this question so when I came across it I noticed that the definition(s) provided here
The public final field length, which contains the number of components of the array.
is not "exactly" correct.
The field length contains the number of available places to put a component, not the number of components present in the array. So it represents the total available memory allocated to that array, not how much of that memory is filled.
Example:
static class StuffClass {
int stuff;
StuffClass(int stuff) {
this.stuff = stuff;
}
}
public static void main(String[] args) {
int[] test = new int[5];
test[0] = 2;
test[1] = 33;
System.out.println("Length of int[]:\t" + test.length);
String[] test2 = new String[5];
test2[0] = "2";
test2[1] = "33";
System.out.println("Length of String[]:\t" + test2.length);
StuffClass[] test3 = new StuffClass[5];
test3[0] = new StuffClass(2);
test3[1] = new StuffClass(33);
System.out.println("Length of StuffClass[]:\t" + test3.length);
}
Output:
Length of int[]: 5
Length of String[]: 5
Length of StuffClass[]: 5
However, the .size() property of the ArrayList does give the number of elements in the list:
ArrayList<Integer> intsList = new ArrayList<Integer>();
System.out.println("List size:\t" + intsList.size());
intsList.add(2);
System.out.println("List size:\t" + intsList.size());
intsList.add(33);
System.out.println("List size:\t" + intsList.size());
Output:
List size: 0
List size: 1
List size: 2
it's public final field , which contains the number of components of the array (length may be positive or zero)
An array thus has the same public fields and methods as the following class:
class A implements Cloneable, java.io.Serializable {
public final int length = X;
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e.getMessage());
}
}
}
more info at
10.7 Array Members
http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html
To answer it as it-is, where is this length property of array defined? In a special Object header.
Easy to see via JOL
int [] ints = new int[23];
System.out.println(ClassLayout.parseInstance(ints).toPrintable());
One of the lines from this output is going to be:
OFFSET SIZE TYPE DESCRIPTION
16 4 (object header) 17 00 00 00 (00010111 00000000 00000000 00000000) (23)
Usually Objects have two headers (mark and klass), arrays have one more that always occupy 4 bytes in length, as size is an int.
The keyword length acts like a data filed defined. When using in an array, we can use it to access how many elements in an array. Regarding to String[], we can invoke length() method defined in String class. With regard to ArrayList, we can use size() method defined in ArrayList. Note that when creating an array list with ArrayList<>(capacity), the initial size() of this array list is zero since there is no element.
I am fairly new to Java and was wondering what the difference between the two is. For this example I used arrays:
class testpile {
public static void main(String[] args)
{
int[] a = {1,2,3,4,5,6}; //First array
int[] b = new int[5]; //Second Array
b[0] = 7;
b[1] = 8;
b[2] = 9;
b[3] = 10;
b[4] = 11;
print(a);
print(b);
}
public static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
I understand that using "new" creates a unique object but what are the advantages of using one over the other?
In your example there's no real difference between the two. The first is mostly just "syntactic sugar" for the latter. In both cases the array is allocated on the heap.
Both of the code creating a int array of size 5/6
In the first case the array is initialized with vale at the time of creation
In second case the value is assigned latter
that's the difference
I understand that using "new" creates a unique object but what are the advantages of using one over the other?
Both constructs do exactly the same thing (with different data, though): Creating an array and filling it with data. In particular, both these arrays are "unique objects".
You'd use the "less literal" one when you do not know the size and the initial values for the element at compile-time.
int[] a = {1,2,3,4,5,6}; //First array
int[] b = new int[5]; //Second Array
They are just two different ways of creating an array. There isn't really any OOP involved here.
The first one is better when you know the values before hand, otherwise the second is better.
The first statement is called array initialization where six int variables are created and each variable is assigned. In second statement, the new keyword create 5 int variables whose initial value is zero.
Using new keyword, you may instantiate an array whenever you require.
int []a=new int[5];
for(int i:a)
System.out.println(i);
a=new int[]{11,22,33};
for(int i:a)
System.out.println(i);
I think the result is same.
But when you create a array with "new" clause, You should assign a specify length of the array.
e.g int[] b = new int[**5**];
And in this sample, you can also assign the value for b[5]. there shouldn't produce compilation error.But the error should occur in the runtime.
In regard to the another method, the length of the array don't need specify. It depend on the element count of array.
In the code below, I don't seem to understand the limitations of the curly bracket initialisation. What do they actually do? It seems in the case of A it just sets a[0] equal to the value directly. In the case of b it using implicit conversion. Does it decide which one to do based on what is available, or is there some other method it uses?
#include <iostream>
using namespace std;
struct A
{
};
struct B
{
B(int a) { cout << a; }
};
int main()
{
A* a[] = {new A()};
B b[] = {1};
}
Also would this type of curly bracket initialisation work similarly in Java?
public class A
{
public static void main(String[] args)
{
someClass[] sC = { /* what can go here? an argument to the constructor,
or just a value to set the variable equal to */ }.
}
}
Sorry if my question seems silly, just really want to know more about curly brackets in c++ and Java. Thanks in advance :-)
Since the Java part has already been answered, I will add a bit about the C++ part. The specific version of curly brace initialization that you refer to is called aggregate initialization and (unsurprisingly) is used to initialize aggregates. Each element in the aggregate will be initialized with the corresponding element inside the braces and you can use whatever you want to use that can be implicitly convertible to the type of the element in the aggregate.
There are a couple of specific parts of the feature that you might want to consider for the specific case of arrays. The number of elements inside the curly braces cannot be greater than the size of the array, but it can be smaller in which case the rest of the elements will be default initialized.
int a[5] = { 1, 2 }; // [ 1, 2, 0, 0, 0 ]
If the size of the array is not provided in user code, the compiler will set it to the number of elements in the aggregate-initialization list:
int a[] = { 1, 2, 3 }; // int a[3]
Note that unlike in Java, the size is an integral part of the type of the array, so that while you can type int a[] = { 1 };, it can never be a generic array of undetermined number of int.
In C++11 the curly brace syntax has been extended to provide uniform initialization, but that is probably outside of the scope of the question, I just mention it in case you want to read more on the subject.
It's the same thing as in C++
someClass[] sC = { new someClass(), new someClass(), new someClass() };
int[] i = { 1, 2, 3 };
String[] s = { "1", "2", "3" };
I don't quite remember how it is done in C++, but in java you can do:
String[] array = new String[]{ "a", "b", "c" };
So you don't pass arguments to the constructors, you pass the objects themselves.
In Java, array is a container object.
You can store values for primitive types and object references of someClass or its sub-classes into array of someClass[] sC.
class SomeClass {}
class Foo extends SomeClass{}
Foo f=new Foo();
SomeClass []sC={f,new Foo(),new SomeClass()};
Can someone explain why the last assignment in the code below is not valid
class ScjpTest extends BounceObject implements Bouncable{
static int ac = 5;
static char ab = 'd';
static int[] a = new int[]{1,2,3};
static int[] b = new int[]{1,2,3};
static char[] c = new char[]{'a','b'};
public static void main(String[] args){
a = b;
b = a;
ac = ab; //This is accepted
a = c; //This is rejected
}
}
The compiler complains with the following error
ScjpTest.java:10: incompatible types
found : char[]
required: int[]
a = c;
^
1 error
The following is also accepted
class Animal{}
class Horse extends Animal{]
Animal[] animals = new Animal[2];
Horse[] horses = new Horses[2];
animals = horses;
Why can i then not assign a char array to an int array?
Because the language specification forbids it. It works at runtime to view a String[] as an Object[] because both have the same representation in memory. But char[] and int[] have different memory representations, and it would be unacceptably slow if every operation on an int[] had to check whether it was really a char[] in disguise.
Arrays of reference type do have to do runtime checks for assignments in order to know whether to throw ArrayStoreException, but at least reads from them is independent of the element type.
The array is a type itself, and casting doesn't work the same way as it primitives. So you have to loop all elements and assign them to the respective element of the other array.
You can assign a char variable to an int one, because the compiler applies a broadening conversion. The basis being that an int is larger than a char and as such there is no chance of loss of information in the conversion. If you tried to assign your int to your char though, you would notice the compiler rejects your code until you put in a specific typecast. This is because narrowing conversions almost always involve a loss of data, and as such the compiler requires you, the programmer, to explicitely indicate this is what you intend to do.
The same principle applies to objects. A broadening conversion is allowed implicitely, a narrowing conversion requires a typecast. Note that at no time can you convert objects that aren't related to each other by super-class/sub-class hierarchy.
Number n = null;
Integer i = null;
n = i ; // Allowed without casting, Number is superclass of Integer
i = n; // Compiler error, Integer is sub-class of Number
i= (Integer)n; // Allowed due to the type-cast
In the example above, Number is a superclass of Integer. Assigning an instance of Integer to instance of Number is allowed without typecast, since this is a 'broadening' conversion. Assigning an instance of Number to instance of Integer requires a specific cast since this is a narrowing conversion (Number could have represented a Float, or some other subclass). These rules carry over to arrays. So, you can do the following:
Number[] numArr = null;
Integer[] intArr = null;
numArr = intArr;
intArr = numArr; //Compile error
intArr = (Integer[]) numArr;
For primitive arrays, there is no broadening conversion done on the array elements, even in cases where you think that it might make sense (ie, char to int, or byte to char etc). While this might not seem to make sense, if you look at the object analogue it becomes clearer why:
Double[] doubleArr = null;
Integer[] intArr = null;
doubleArr = intArr ; // Compile error, Double,Integer are sibling classes
doubleArr = (Double[]) intArr; // Compile error, same reason
intArr = doubleArr; // Compile error, same reason
intArr = (Integer[]) doubleArr; // Compile error, same reason
You can not cast a Double to an Integer, or vice-versa, they are completely different classes from the point of view of the compiler (as different as String and Float would be, for example).