java 帮助尝试理解圆形阵列中的模运算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5964771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help trying to understand modulo operation in circular array
提问by JBoy
i have a small issue trying to figure out how a modulo operation is being calculated. I am building up a queue, so i have a circular array. i cannot figure out how this modulo operation works.
我有一个小问题试图弄清楚模运算是如何计算的。我正在建立一个队列,所以我有一个循环数组。我无法弄清楚这个模运算是如何工作的。
Given q: an array of Character of 5 elements length, The MAX constant gives the max length of the array "5" rare is an int which represents the first available spot in the array q
给定 q:一个长度为 5 个元素的字符数组,MAX 常量给出了数组的最大长度 "5" 罕见是一个整数,它表示数组 q 中的第一个可用位置
public void enqueue(Character c)throws FullQueueException{
if(size()== MAX -1){ //if only 1 place left, is full, throw exc
throw new FullQueueException("Queue is full");
}
q[rare]=c;
rare=(rare+1)%MAX;
}
Now, supposing that the rare "first empty spot" is three, what is the rare value going to be after the method has finished? this is what i dont get, rare=(rare+1)%MAX means rare=4%5 which gives rare=0,8.
现在,假设稀有的“第一个空位”是三个,那么方法完成后稀有值将是多少?这是我没有得到的,rare=(rare+1)%MAX 表示rare=4%5,这给出了rare=0,8。
Same for method size:
方法大小相同:
public int size() {
return (MAX - front + rear) % MAX;
}
Given, front, an int variable which represents the first element in the array Suppose front is 1 and rare 4, so there are 3 elements in the array, so size is (5-1+4)%5 which is 8%5 which gives 1.6, while the actual size is 3 Any suggestion? this might be more math then java but probably some of you came across the same doubt before. Thank you!
给定,front,一个表示数组中第一个元素的 int 变量假设前面是 1 和罕见的 4,所以数组中有 3 个元素,所以大小是 (5-1+4)%5 也就是 8%5给出 1.6,而实际大小是 3 有什么建议吗?这可能比 java 更数学,但可能你们中的一些人之前遇到过同样的疑问。谢谢!
回答by tschaible
I think you're a bit conofused as to what the modulo operation does. It gives the integer remainder after a division. So from your example.
我认为您对模运算的作用有点困惑。它给出除法后的整数余数。所以从你的例子来看。
4 % 5 = 4 (because 4/5 is 0, with a remainder of 4)
4 % 5 = 4(因为 4/5 为 0,余数为 4)
AND
和
8 % 5 = 3 (because 8/5 is 1 with a remainder of 3)
8 % 5 = 3(因为 8/5 是 1,余数是 3)
Without seeing the rest of your implementation, its a bit difficult to explain further why modulo is being used, but it looks like its basically being used to ensure that your circular array wraps around. i.e. when you hit the end of the array (say index 7, of an array with MAX size 8, the next value you would want would be the first element, which would be 8%8 or 0).
在没有看到您的其余实现的情况下,进一步解释为什么使用模有点困难,但看起来它基本上用于确保您的圆形阵列环绕。即,当您到达数组的末尾时(例如,最大大小为 8 的数组的索引 7,您想要的下一个值将是第一个元素,即 8%8 或 0)。
回答by Peter Lawrey
integer arithmetic will only result in integers. While modulo is related to division it is not division.
整数算术只会产生整数。虽然模与除法有关,但它不是除法。
a % b is the same as (a - a / b * b)
As loops it is the same as. (Assuming b is positive)
作为循环,它是一样的。(假设 b 为正)
int result = a;
while(a >= b) a -= b;
while(a + b <= 0) a += b;
However for rare = (rare + 1) % MAX
, it is the same as.
然而对于rare = (rare + 1) % MAX
,它是一样的。
rare = (rare == MAX - 1 ? 0 : rare + 1);