Erinevus lehekülje "JavaPython:List" redaktsioonide vahel

Allikas: Kursused
Mine navigeerimisribale Mine otsikasti
1. rida: 1. rida:
 
{{JavaPython-sisukord}}
 
{{JavaPython-sisukord}}
  
 +
Pythoni järjendile on Javas lähim sarnane andmetüüp kas array või ArrayList.
  
 +
== Python järjend --> Java array ==
 +
An array is indexed like a Python list, with <b>square brackets</b>: <pre>students[0] or letters[i][j].</pre>
 +
 +
Declaring an array does not create one. The declaration tells how many dimensions the array has, but never its size: <pre>for example, char[][] letters.</pre> To create one, use the word new, then its type and size; <pre>new char[5][5].</pre>
 +
 +
Python has list literals: <pre>[1, 2, 3, 4]. </pre>Java has two different syntaxes for array literals:
 +
 +
In a declaration (only): <pre>int[] numbers = {1, 2, 3, 4};</pre>
 +
Other places: <pre>new int[] {1, 2, 3, 4} </pre>
 +
Java has no equivalent to Python's list[i:j] notation.
 +
 +
== Python järjend --> Java ArrayList ==
 +
 +
An ArrayList acts more like a Python list, but uses only object syntax. There are no literals.
 +
 +
<pre>ArrayList<String> languages = new ArrayList<String>();
 +
languages.add("Python");  // append to list
 +
String oldLanguage = languages.get(0);
 +
languages.set(0, "Java");
 +
Some additional methods: boolean isEmpty(), boolean contains(object), int size(), type remove(index).
 +
</pre>
 
== Näide ==
 
== Näide ==
  

Redaktsioon: 2. veebruar 2016, kell 10:56

Java vs Python

Pythoni järjendile on Javas lähim sarnane andmetüüp kas array või ArrayList.

Python järjend --> Java array

An array is indexed like a Python list, with square brackets:

students[0] or letters[i][j].

Declaring an array does not create one. The declaration tells how many dimensions the array has, but never its size:

for example, char[][] letters.

To create one, use the word new, then its type and size;

new char[5][5].

Python has list literals:

[1, 2, 3, 4]. 

Java has two different syntaxes for array literals: In a declaration (only):

int[] numbers = {1, 2, 3, 4};

Other places:

new int[] {1, 2, 3, 4} 

Java has no equivalent to Python's list[i:j] notation.

Python järjend --> Java ArrayList

An ArrayList acts more like a Python list, but uses only object syntax. There are no literals.

ArrayList<String> languages = new ArrayList<String>();
languages.add("Python");  // append to list
String oldLanguage = languages.get(0);
languages.set(0, "Java");
Some additional methods: boolean isEmpty(), boolean contains(object), int size(), type remove(index).

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>