Array ArrayList python等价物

时间:2015-09-25 01:40:14

标签: java python arrays arraylist

我只是查找数组 arrayList

并发现数组是固定长度的,无法更改,而 arraylist 可以更改且长度可变

我的问题是:

在python中数组 == 元组

并且在python中 arraylist == 列表

如果他们不是数组和arraylist的python等价物?

2 个答案:

答案 0 :(得分:8)

java中的

public static void main(String[] args) { int guess; int magic = (int) (Math.random() * 10 + 1); int attempt1 = 1; int attempt2 = 1; int attempt3 = 1; int players; players = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Players. \n2-3 Only")); if(players == 2) { do { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 1, Enter A Number from 1-10","Guessing Game",1)); if(guess == magic) JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)","Guessing Game",1); else { attempt1++; if(guess < magic) JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!" ,"Guessing Game",0 ); else JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!" ,"Guessing Game",0); } }while(guess != magic); do { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 2, Enter A Number from 1-10","Guessing Game",1)); if(guess == magic) JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)","Guessing Game",1); else { attempt2++; if(guess < magic) JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!" ,"Guessing Game",0 ); else JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!" ,"Guessing Game",0); } }while(guess != magic); if(attempt1<attempt2) JOptionPane.showMessageDialog(null, "Player 1 Won with " + attempt1 + "attempts" ,"Guessing Game",0); else JOptionPane.showMessageDialog(null, "Player 2 Won Won with " + attempt2 + "attempts" ,"Guessing Game",0); } else if(players == 3) { do { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 1, Enter A Number from 1-10","Guessing Game",1)); if(guess == magic) JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)","Guessing Game",1); else { attempt1++; if(guess < magic) JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!" ,"Guessing Game",0 ); else JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!" ,"Guessing Game",0); } }while(guess != magic); do { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 2, Enter A Number from 1-10","Guessing Game",1)); if(guess == magic) JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)","Guessing Game",1); else { attempt2++; if(guess < magic) JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!" ,"Guessing Game",0 ); else JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!" ,"Guessing Game",0); } }while(guess != magic); do { guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 3, Enter A Number from 1-10","Guessing Game",1)); if(guess == magic) JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)","Guessing Game",1); else { attempt3++; if(guess < magic) JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!" ,"Guessing Game",0 ); else JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!" ,"Guessing Game",0); } }while(guess != magic); if(attempt1 < attempt2) if(attempt1 < attempt3) JOptionPane.showMessageDialog(null, "Player 1 Won with " + attempt1 + "attempts" ,"Guessing Game",0); else if(attempt2 < attempt3) if(attempt2 < attempt1) JOptionPane.showMessageDialog(null, "Player 2 Won with " + attempt2 + "attempts" ,"Guessing Game",0); else if(attempt3 < attempt2) if(attempt3 < attempt1) JOptionPane.showMessageDialog(null, "Player 3 Won with " + attempt3 + "attempts" ,"Guessing Game",0); } } 和python中的ArrayList都是动态数组。它们都具有O(1)平均索引时间和O(1)平均值,在结束时间添加元素。

Python中的

list在python中 Array。虽然您无法向两个数据结构添加元素。 Python tuple不支持赋值,即您无法在tuple中重新分配单个元素,而在tuple中则可以。

答案 1 :(得分:1)

  • Java的ArrayList类似于Python的List。
  • 比数组更适合添加和删除项目。
  • Java的数组具有与您相似的固定长度 提及。
  • 不确定Python中的等价物是什么。