Below , we have listed the difference between array operations between Python and Java.
Python
|
Java
|
Python Array Declaration of 2 integers
|
Java Array Decalaration of 2 integers
|
>>> arr = [0] * 2
>>> arr[0] 0 >>> arr[0] = 10 >>> arr[1] = 2 >>> arr[2] = 9 Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list assignment index out of range >>> arr.append(2) # this feature is not available in Java primitive arrays >>> |
public class hello_world {
public static void main(String[] args) { int[] arr = new int[2]; arr[0] = 10; arr[1] = 9; arr[2] = 5; System.out.println(arr[0] + " " + arr[1]); } } >> Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at com.dmac.analytics.spark.hello_world.main(hello_world.java:9) |
We can append values to an existing array in python.
|
In Java, a traditional array has to be reassigned to a new array
with larger space allocated.
|
Java also has a library that can use create a list that is mutable.
Read up on java.util.List() to know more about the java data structure.
Please let us know if you like our posts. Like us on Google+ and Facebook.
No comments:
Post a Comment
Please share your thoughts and let us know the topics you want covered