Tag: for loop

增强的For循环exception

在玩循环时创建以下代码。 下面的代码将Fibonacci值存储到一个数组中,然后使用for循环打印它们。 int [] numbers; numbers = new int[25]; numbers[0] = 1; numbers[1] = 1; System.out.println(“Initializing the array values”); for(int i = 2; i < numbers.length; i++) { numbers[i] = numbers[i-1] + numbers[i-2]; } System.out.println("Printing out Fibonacci values"); for(int i = 0; i < numbers.length; i++) { System.out.print(numbers[i] + ", "); } 上面的代码工作正常。 我第一次把它扔到一起,我用一个增强的for循环来打印出值(代码中的第二个for循环)。 编译很好,但是当我运行它时,我得到以下内容; […]

为什么我不能以这种方式在for循环中定义变量?

通过测试,我发现, 这不合法: for (int i= 0; i < 1000000000; i++) int j = i & i+1 ; // j cannot be declared here! 但这是: for (int i= 0; i < 1000000000; i++) { int j = i & i+1 ; } 为什么?

Java – 使用嵌套for循环创建带数字的三角形

我有功课要做一个看起来像这样的三角形: 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6 我已经能够使用以下代码创建几乎一半的三角形: public static void main(String[] args) { for (int i = 1; i <= 6; i++) { for […]

Java 8中的legacy for循环,流和parallelStream之间的比较

import java.util.ArrayList; import java.util.List; public class IterationBenchmark { public static void main(String args[]){ List persons = new ArrayList(); persons.add(“AAA”); persons.add(“BBB”); persons.add(“CCC”); persons.add(“DDD”); long timeMillis = System.currentTimeMillis(); for(String person : persons) System.out.println(person); System.out.println(“Time taken for legacy for loop : “+ (System.currentTimeMillis() – timeMillis)); timeMillis = System.currentTimeMillis(); persons.stream().forEach(System.out::println); System.out.println(“Time taken for sequence stream : “+ (System.currentTimeMillis() – […]

Java – 用于循环声明的逗号运算符

我知道我可以像这样使用逗号运算符 for (int i = 1, j = 15; j>10; i++, j–) { // do something neat } 但是有些文章似乎暗示逗号运算符可以在for循环声明之外使用,例如 int j = 2, k = 4 ; int x ; // Assignment statement with comma operator x = j + 1, k ; 来源: http : //www.cs.umd.edu/~clin/MoreJava/ControlFlow/comma.html 要么 int x = (expression) ? (i++,2) : […]

如何使用foreach循环遍历java 8流

假设我们尝试将一个可能抛出已检查exception的lambda应用于java 8流: Stream stream = Stream.of(“1”, “2”, “3”); Writer writer = new FileWriter(“example.txt”); stream.forEach(s -> writer.append(s)); // Unhandled exception: java.io.IOException 这不会编译。 一种解决方法是在RuntimeException嵌套已检查的exception,但它使以后的exception处理变得复杂,而且它只是丑陋: stream.forEach(s -> { try { writer.append(s); } catch (IOException e) { throw new RuntimeException(e); } }); 替代解决方法可能是将有限的function性forEach转换为对检查exception更友好的普通旧foreach 循环 。 但天真的方法失败了: for (String s : stream) { // for-each not applicable to expression type […]

通过二维arrays对角循环

我写了下面的代码来走一半数组的对角线: String[][] b = [a,b,c] [d,e,f] [g,h,i]; public void LoopDiag() for (int i = b.length – 1; i > 0; i–) { String temp = “”; for (int j = 0, x = i; x <= b.length – 1; j++, x++) { temp = temp+b[x][j]; } System.out.println(temp) } for (int i = 0; i <= […]

lambda表现的差异?

这不是我的问题的重复。 我检查了它,它更多的是关于内部匿名类。 我对Lambda表达式很好奇并测试了以下内容: 给定一万个条目的数组,删除某些索引的速度会更快:Lamba表达式还是带有if测试的For-Loop? 第一个结果并不令人惊讶,因为我不知道我会想出什么: final int NUMBER_OF_LIST_INDEXES = 10_000; List myList = new ArrayList(); String[] myWords = “Testing Lamba expressions with this String array”.split(” “); for (int i = 0 ; i x.contains(“s”)); // 16 milliseconds for the traditional Loop for (int i = NUMBER_OF_LIST_INDEXES – 1 ; i >= 0 ; i–){ if […]

java中的foreach关键字?

我知道foreach在编程中的含义以及何时使用它。 Java中是否有foreach关键字? 我试图找到一个关键字列表,但只有foreach而不是foreach 。

Java“for”语句实现可防止垃圾回收

UPD 21.11.2017:该错误已在JDK中修复,请参阅Vicente Romero的评论 概要: 如果for语句用于任何Iterable实现,则集合将保留在堆内存中,直到当前作用域(方法,语句体)结束,即使您没有对集合的任何其他引用,也不会进行垃圾回收。应用程序需要分配新内存。 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8175883 https://bugs.openjdk.java.net/browse/JDK-8175883 例子 : 如果我有下一个代码,它会分配一个包含随机内容的大字符串列表: import java.util.ArrayList; public class IteratorAndGc { // number of strings and the size of every string static final int N = 7500; public static void main(String[] args) { System.gc(); gcInMethod(); System.gc(); showMemoryUsage(“GC after the method body”); ArrayList strings2 = generateLargeStringsArray(N); showMemoryUsage(“Third allocation outside the method […]