简体   繁体   English

数组逻辑:如何排列两个数组编号

[英]array logic : How can i arrange two array numbers

I have two arrays:我有两个数组:

int array1 [] = {1,2,3,4,5,6,7}; [This array can grow to huge numbers] [这个数组可以增长到巨大的数字]

int array2 [] = {1,2,3}; [Max it can have is 30 elements] [最多可以有 30 个元素]

array1 should be the parent loop and will have more elements than array2. array1 应该是父循环,并且比 array2 有更多的元素。 For example I have to arrange like below :例如,我必须安排如下:

Expected result :预期结果 :
1-1 1-1
2-2 2-2
3-3 3-3
4-1 4-1
5-2 5-2
6-3 6-3
7-1 7-1

How can I proceed?我该如何继续?

You don't need a nested loop in this case;在这种情况下,您不需要嵌套循环; all you need is proper use of the modulus operator.您所需要的只是正确使用模数运算符。

for (int i = 0; i < array1.length; i++) {
  System.out.println(array1[i] + "-" + array2[i % array2.length]);
}

The reason this works is that the modulus operator maps very nicely to your problem.这样做的原因是模数运算符很好地映射到您的问题。 The operation you're trying to perform requires repeatedly iterating through a list ( array2 ) and wrapping back to the beginning.您尝试执行的操作需要重复遍历列表 ( array2 ) 并返回到开头。 The modulus operator is, in essence, a "wrapping" operator;取模运算符本质上是一个“包装”运算符; thinking incrementally, it allows you to go back to 0 every time you reach a certain number (in this case, the size of array2 ).循序渐进地思考,它允许您在每次达到某个数字(在本例中为array2的大小)时返回到 0。

So you have two sets, you want to loop through each element of the larger set (array1) and for each element of the set print the element, and the next element of set2 (array2).所以你有两个集合,你想遍历较大集合 (array1) 的每个元素,并为集合的每个元素打印元素,以及 set2 (array2) 的下一个元素。

I used an enhanced for loop , it loops over each element, and then modulus witch effectively limits i to be less than the length of set2.我使用了增强的 for 循环,它循环遍历每个元素,然后模数有效地将i限制为小于 set2 的长度。 (remainder must be less than 3 if dividing by 3) (如果除以 3,余数必须小于 3)

This allows us to add one to i using i++ and be assured that it will always loop back around to the first element as if the value were on a spinner .这允许我们使用i++将一个添加到i并确保它将始终循环回到第一个元素,就好像该值在spinner 上一样

  int i = 0;
  for( int value : array1 ) {
     System.out.print( value + "-" + array2[i++ % array2.length] );
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在阵列中排列从500到1的数组? (JAVA) - How can I arrange an array from 500 down to 1 in an array? (Java) 如何用两个数字之间的所有值填充数组? - How can I fill an array with all of the values between two numbers? 如何在数组中添加随机数? - How can i add random numbers in an array? 用于排列数字数组的代码片段中的逻辑错误 - Logical error in a code snippet to arrange an array of numbers 将数字排列到3维数组的最快方法是什么? - What is the quickest way to arrange numbers to 3 dimensional array? 如何排列数字的数字,以便当我将其分成两个相同数字的数字时,两个数字的乘积最大? - How to arrange the digits of a number such that when I break it into two numbers of equal digits the product of the two numbers is maximum? 如何使用 for 和 if 语句将两个 arrays 中的匹配数字添加到空的第三个数组中? - How can I add the matching numbers from two arrays to an empty third array using for and if statements? 如何获得两个数字之间的最大素数并将其存储在数组中? - How can I get largest prime factors between two numbers and store them in an array? 如何在数组中安排 firebase 检索? - How to arrange the firebase retrieval in an array? 如何从给定数组中取出奇数和偶数,然后将它们存储在另一个数组中? - How can I pick out the odd numbers and even numbers from a given array and then store them in another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM