Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

An array is a data structure that with 'slots' that are accessed using an integer index, typically starting on 0 or 1. Python's list is essentially an array - Java has two similar data structures. First The first is an the native array type, which is a list with only a set has a fixed number of ' slots', and . The second is an the ArrayList, which is the closest to Python's list, since it supports changing the length. Notice that for both, you need to declare what type of variables it's going to be holding.

Declaration

Arrays in Java native arrays and Python are lists can both displayed using curly brackets, the difference is that they are called Iists in Pythoncreated using curly braces {...}.

Code Block
languagejava
titleArrays in Java
 int[] Aa = new int[]{3, 5, 7}; // or just {3, 4, 7}
 Aa[0] = 1; // Aa =is {1, 5, 8}
 Aa = new int[5]; // Aa =is {0, 0, 0, 0, 0}
Code Block
languagepy
titleArrays in Python
 A = ["spam", "baked beans", "sausage", "spam"]
 B = A[0:2] # B =is ["spam", "baked beans"]
 B[1] = "spam" # B =is ["spam", "spam"]
 B = B + ["Eric"] # B =is ["spam�spam", "spam", "Eric"]

In Java, Strings strings are not arrays, as in Python.   You need to use the String class and methods to access individual characters, while in Python,  Strings strings can be treated as immutable arrays of characters.

Code Block
languagejava
titleStrings in Java
 String s = "spam";
 char c = s.charAt(1); // c is 'p'
Code Block
languagepy
titleStrings in Python
 S = "spam"
 c = S[1] # c = "p"