对于Java语言有些新意,我试图让自己熟悉一个可能遍历列表(或者可能是其他集合)以及每个集合的优点或缺点的所有方法(或者至少是非病态方法).
给定一个List<E> list对象,我知道以下循环所有元素的方法:
while/ do while循环以及)// Not recommended (see below)!
for (int i = 0; i < list.size(); i++) {
E element = list.get(i);
// 1 - can call methods of element
// 2 - can use 'i' to make index-based calls to methods of list
// ...
}
Run Code Online (Sandbox Code Playgroud)
注意:正如@amarseillan指出的那样,这种形式对于迭代Lists来说是一个糟糕的选择,因为该get方法的实际实现可能不如使用时那样有效Iterator.例如,LinkedList实现必须遍历i之前的所有元素以获得第i个元素.
在上面的例子中,List实现没有办法"保存它的位置"以使未来的迭代更有效.因为ArrayList它并不重要,因为复杂性/成本get是恒定时间(O(1)),而a LinkedList是它与列表的大小(O(n))成比例.
有关内置Collections实现的计算复杂性的更多信息,请查看此问题 …
我正在编写一个必须接受用户输入的程序.
#note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input`
age = int(input("Please enter your age: "))
if age >= 18:
print("You are able to vote in the United States!")
else:
print("You are not able to vote in the United States.")
Run Code Online (Sandbox Code Playgroud)
如果用户输入合理数据,这将按预期工作.
C:\Python\Projects> canyouvote.py
Please enter your age: 23
You are able to vote in the United States!
Run Code Online (Sandbox Code Playgroud)
但如果他们犯了错误,那就崩溃了:
C:\Python\Projects> canyouvote.py
Please enter your age: dickety six
Traceback (most recent call last):
File "canyouvote.py", line 1, in …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
public class Tests {
public static void main(String[] args) throws Exception {
int x = 0;
while(x<3) {
x = x++;
System.out.println(x);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我们知道他应该只是写x++或者x=x+1,但是x = x++它首先要归于x自身,然后再增加它.为什么x继续0作为价值?
--update
这是字节码:
public class Tests extends java.lang.Object{
public Tests();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]) throws java.lang.Exception;
Code:
0: iconst_0
1: istore_1
2: iload_1
3: iconst_3
4: if_icmpge 22
7: iload_1 …Run Code Online (Sandbox Code Playgroud) 我有以下JSON结构:
[{ "id":"10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }]
Run Code Online (Sandbox Code Playgroud)
如何使用jQuery或JavaScript迭代它?
问题很简单.foreach我的代码中有一个循环:
foreach($array as $element) {
//code
}
Run Code Online (Sandbox Code Playgroud)
在这个循环中,我想在第一次或最后一次迭代时做出不同的反应.
这该怎么做?
我有一个Python脚本,它将整数列表作为输入,我需要一次使用四个整数.不幸的是,我无法控制输入,或者我将它作为四元素元组列表传入.目前,我正在以这种方式迭代它:
for i in xrange(0, len(ints), 4):
# dummy op for example code
foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3]
Run Code Online (Sandbox Code Playgroud)
它看起来很像"C-think",这让我怀疑有更多的pythonic方式来处理这种情况.迭代后会丢弃该列表,因此无需保留.也许这样的事情会更好吗?
while ints:
foo += ints[0] * ints[1] + ints[2] * ints[3]
ints[0:4] = []
Run Code Online (Sandbox Code Playgroud)
但是,仍然没有"感觉"正确.: - /
我正在使用此代码让用户输入名称,而程序将它们存储在一个数组中,直到它们输入一个空字符串(他们必须在每个名称后按Enter键):
people = []
info = 'a' # must fill variable with something, otherwise loop won't execute
while not info.empty?
info = gets.chomp
people += [Person.new(info)] if not info.empty?
end
Run Code Online (Sandbox Code Playgroud)
这个代码在do ... while循环中看起来更好看:
people = []
do
info = gets.chomp
people += [Person.new(info)] if not info.empty?
while not info.empty?
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我不必将信息分配给一些随机字符串.
不幸的是,Ruby中似乎不存在这种类型的循环.任何人都可以建议一个更好的方法吗?
有可能找到foreach索引吗?
在一个for循环如下:
for ($i = 0; $i < 10; ++$i) {
echo $i . ' ';
}
Run Code Online (Sandbox Code Playgroud)
$i 会给你索引.
我是否必须使用for循环或是否有某种方法来获取foreach循环中的索引?
我有以下for循环,当我splice()用来删除一个项目时,我得到'秒'未定义.我可以检查它是否未定义,但我觉得这可能是一种更优雅的方式.希望简单地删除一个项目并继续前进.
for (i = 0, len = Auction.auctions.length; i < len; i++) {
auction = Auction.auctions[i];
Auction.auctions[i]['seconds'] --;
if (auction.seconds < 0) {
Auction.auctions.splice(i, 1);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在寻找一个更好的模式来处理每个需要处理的元素列表,然后根据结果从列表中删除.
你不能.Remove(element)在里面使用foreach (var element in X)(因为它导致Collection was modified; enumeration operation may not execute.异常)...你也不能使用for (int i = 0; i < elements.Count(); i++),.RemoveAt(i)因为它会扰乱你在集合中的当前位置i.
有一种优雅的方式来做到这一点?
loops ×10
foreach ×2
java ×2
javascript ×2
list ×2
php ×2
python ×2
c# ×1
chunks ×1
collections ×1
generics ×1
increment ×1
iteration ×1
json ×1
key-value ×1
operators ×1
optimization ×1
python-3.x ×1
ruby ×1
user-input ×1
validation ×1