CSC300: More about Arrays [10/12] Previous pageContentsNext page

java.util.Arrays includes a useful function for printing arrays:

01
02
03
04
05
06
07
08
09
10
package algs11;
import stdlib.*;
import java.util.Arrays;
public class Hello {
  public static void main (String[] args) {
    double[] lst = { 11, 21, 31 };
    StdOut.println (lst);
    StdOut.println (Arrays.toString(lst));
  }
}

The size of a Java array is fixed when it is created.

PythonJava
01
02
03
04
05
06
07
08
09
10
11
12
13
def createList(size):
    lst = []
    for i in range(size):
        lst.append(0)
    return lst

def main():
    lst = createList(3)
    lst[1] = 21
    print (lst)

 if __name__ == "__main__":
    main()
01
02
03
04
05
06
07
08
09
10
11
12
13
package algs11;
import stdlib.*;
import java.util.Arrays;
public class Hello {
  public static double[] createList (int size) {
    return new double[size];
  }
  public static void main (String[] args) {
    double[] lst = createList(3);
    lst[1] = 21;
    StdOut.println(Arrays.toString(lst));
  }
}

Previous pageContentsNext page