Tag: for loop

在循环中更改图像 – Android

我想知道为什么我仍然无法想办法做到这一点。 虽然看起来很简单,但我花了整整一天的时间。 但不能这样做。 我有一组骰子图像。 1.png,2.png,….和6.png。 我的布局中有一个ImageView。 那是, ImageView dice = (ImageView) findViewById(R.id.imageViewrollingdiceOne); 在这里,我想快速更改此imageView以查看使用6个以上图像的某种视觉/动画。 为此我写下了一段代码。 代码1: for (int j=0;j<10;j++){ int randomNum = random.nextInt(6); System.out.println("Random Value " + randomNum); dice.setImageResource(images[randomNum]); } 输出: 没有视觉效果。 imageView保持不变,并在循环最后一次迭代时突然改变。 我认为这是因为循环执行速度非常快。 然后我做了以下。 代码2: for (int j=0;j<10;j++){ int randomNum = random.nextInt(6); System.out.println("Random Value " + randomNum); dice.setImageResource(images[randomNum]); try { Thread.sleep(200); } catch (InterruptedException e) […]

有没有更有效的方法在for循环中创建新变量?

如果你有一个ArrayList(在这种情况下命名为al ),并且你想要将列表的前五个元素作为变量,你可以这样做: String x1 = al.get(0); String x2 = al.get(1); String x3 = al.get(2); String x4 = al.get(3); String x5 = al.get(4); 但是,使用for循环,有没有办法可以做这样的事情: for (int i = 1; i < 6; i++){ String namer = "x" + i.toString(); String [value of String 'namer'] = al.get(i-1); } 或者是否有一种完全不同的方法更有效?

搜索字符串是否有一个字符

我正在尝试确定输入的单词是否因文本文件中的一个字符而异。 我有适用的代码,但不幸的是只有两个字符或更少的字,显然不是很有用,代码本身看起来有点乱。 这是我到目前为止所拥有的: if(random.length() == word.length()){ for(int i = 0; i < random.length(); i++){ if( (word.charAt(i) == random.charAt(i))){ str += word+"\n"; count++; } } } random是用户输入的单词, word是在文本文件中搜索的单词。 如果我将第二个if语句更改为某些内容 if( (word.charAt(i) == random.charAt(i)) && (word.charAt(i -1) == random.charAt(i-1))) 如果我改变int i为= 1,我似乎得到了更多我想要完成的东西,但是我的代码只搜索前两个字母是否相同而不是如果最后两个字母是好吧,应该做的。

检查字符串是否只是字母+空格?

我想编写一个传递字符串的静态方法,并检查该字符串是否仅由字母和空格组成。 我可以根据需要使用String的方法length()和charAt(i)。 我想的是以下内容:(对于伪代码抱歉) public static boolean onlyLettersSpaces(String s){ for(i=0;i<s.length();i++){ if (s.charAt(i) != a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) { return false; break; }else { return true; } } 我知道我的编码可能有错误,并且可能有更简单的方法,但请告诉我你的建议!

for-each vs for vs while

我想知道在ArrayList或各种List上实现“for-each”循环的最佳方法是什么。 以下哪项实施最好,为什么? 或者有最好的方法吗? 感谢您的帮助。 List values = new ArrayList(); values.add(“one”); values.add(“two”); values.add(“three”); … //#0 for(String value : values) { … } //#1 for(int i = 0; i < values.size(); i++) { String value = values.get(i); … } //#2 for(Iterator it = values.iterator(); it.hasNext(); ) { String value = it.next(); … } //#3 Iterator it = […]

错误:只能遍历数组或java.lang.Iterable的实例

请帮助我,我的错误似乎无法使其工作,因为它只能迭代数组或java.lang.Iterable的实例。 我想创建一个条形码并阅读它并将其添加到word文档中 更新发布 nodeCollection来自com.aspose.words。 import com.aspose.barcode.*; import com.aspose.barcoderecognition.BarCodeReadType; import com.aspose.barcoderecognition.BarCodeReader; import com.aspose.words.Document; import com.aspose.words.DocumentBuilder; import com.aspose.words.ImageType; import com.aspose.words.NodeCollection; import com.aspose.words.NodeType; import com.aspose.words.Shape; try { // Generate barcode image BarCodeBuilder builder = new BarCodeBuilder(); builder.setSymbologyType(Symbology.Code39Standard); builder.setCodeText(“test-123”); String strBarCodeImageSave = “img.jpg”; builder.save(strBarCodeImageSave); // Add the image to a Word doc Document doc = new Document(); DocumentBuilder docBuilder […]

用于字典的java中的Foreach循环

我想通过java中的字典中的每个项目。 澄清我想做什么,这是C#代码 Dictionary LableList = new Dictionary(); foreach (KeyValuePair z in LabelList); 我不知道如何做到这一点是java,例如我这样做了 for(Object z: dic) 但它说它不可迭代。 请指教……

每个循环如何防止空列表?

我在http://www.leepoint.net/notes-java/flow/loops/foreach.html上阅读。 每个相当于 for (int i = 0; i < arr.length; i++) { type var = arr[i]; body-of-loop } 是 for (type var : arr) { body-of-loop } 我的问题是每个循环如何为空列表工作。 我知道对于常规for循环,arr.length将仅计算为0并且循环不会执行。 每个循环怎么样?

在Java中计算数组中的不同值

我正在编写一个代码,我有一个int [a],该方法应该返回唯一值的数量。 示例:{1} = 0个不同的值,{3,3,3} = 0个不同的值,{1,2} = 2个不同的值,{1,2,3,4} = 4个不同的值等。 我不是允许对数组进行排序 。 问题是我的方法可能不起作用。 我的声明有问题,我无法弄清楚。 public class Program { public static void main(String[] args) { int[] a = {1, 2, 3, 1}; System.out.println(differentValuesUnsorted(a)); //run: 4 //should be 3 } public static int differentValuesUnsorted(int[] a) { int values; //values of different numbers if (a.length < 2) […]

在java中,我如何找到第n个Fibonacci数?

确定Fibonacci序列很容易弄清楚: int num = 0; int num2 = 1; int loop; int fibonacci; System.out.print(num2); for (loop = 1; loop <= 10; loop ++) { fibonacci = num + num2; num = num2; num2 = fibonacci; System.out.print(" " + fibonacci); } 我的问题在于试图精确定位指定N的值。如果我想在序列中找到第6个元素,即8,我怎么能找到那个数字,只有那个数字?