Erinevus lehekülje "JavaPython:List" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
(Uus lehekülg: '{{JavaPython-sisukord}}')
 
1. rida: 1. rida:
 
{{JavaPython-sisukord}}
 
{{JavaPython-sisukord}}
 +
 +
== 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']
 +
 +
#append
 +
aList.append(2)
 +
 +
# extend
 +
aList.extend(["new","list"])
 +
 +
print aList
 +
#[1, 'mike', 'john', 2, 'new', 'list']
 +
 +
aList = [0,1,2,3,4,5,6]
 +
# size
 +
print len(aList)
 +
#7
 +
 +
print aList[2]
 +
#2
 +
 +
print aList[0:3]
 +
#[0, 1, 2]
 +
 +
print aList[2:]
 +
#[2, 3, 4, 5, 6]
 +
 +
print aList[-2]
 +
#5
 +
 +
#lists are mutable
 +
aList[0] = 10
 +
print aList
 +
#[10, 1, 2, 3, 4, 5, 6]
 +
</syntaxhighlight>
 +
|}

Redaktsioon: 30. jaanuar 2016, kell 07:41

Java vs Python

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>