JavaPython:List

Allikas: Kursused
Redaktsioon seisuga 2. veebruar 2016, kell 10:47 kasutajalt Triin (arutelu | kaastöö)
Mine navigeerimisribale Mine otsikasti
Java vs Python

In python, string can reside in a pair of single quotes as well as a pair of double quotes. It supports multiplication: "x"*3 is "xxx".

Näide

Java Python
<syntaxhighlight lang="java" line="1" >

//arraylist is closest with list in python ArrayList<Integer> aList =

 new ArrayList<Integer>();

//add aList.add(1); aList.add(3); aList.add(2); aList.add(4);

//index int n = aList.get(0);

//get sub list List<Integer> subList =

 aList.subList(0, 2);

//1, 3 </syntaxhighlight>

<syntaxhighlight lang="python" line="2" >

aList = [] aList = [1, 'mike', 'john']

  1. append

aList.append(2)

  1. extend

aList.extend(["new","list"])

print aList

  1. [1, 'mike', 'john', 2, 'new', 'list']

aList = [0,1,2,3,4,5,6]

  1. size

print len(aList)

  1. 7

print aList[2]

  1. 2

print aList[0:3]

  1. [0, 1, 2]

print aList[2:]

  1. [2, 3, 4, 5, 6]

print aList[-2]

  1. 5
  1. lists are mutable

aList[0] = 10 print aList

  1. [10, 1, 2, 3, 4, 5, 6]

</syntaxhighlight>