i
当我将它传递给方法时为什么不改变?i
方法调用之后的值是0
,但方法仍然返回101
.
class Program
{
static void Main(string[] args)
{
int i = 0;
Console.WriteLine("Before static method running i={0}", i);
int c= SampleClass.ExampleMethod( i);
Console.WriteLine("i={0} - Static method return c={1}",i,c);
}
}
class SampleClass
{
public static int ExampleMethod(int i)
{
i= 101;
Console.WriteLine("Inside static method i={0}",i);
return i;
}
}
Run Code Online (Sandbox Code Playgroud) 这是使用junit4的测试类.
public class TestArrayUtilities {
static final int[] ONE_D_MATRIX = { 12, 34, 45, 6, 7, 85, 67, 43 };
static final int[] ONE_D_MATRIX_REMOVED_ONE= {12, 34, 45, 6, 85, 67, 43};
Run Code Online (Sandbox Code Playgroud)
removeItemWithIndex()
从给定索引和返回数组的数组中删除元素.此测试非常有效.
@Test
public void testRemoveItemWithIndex(){
assertArrayEquals(ArrayUtilities.removeItemWithIndex(ONE_D_MATRIX, 4),ONE_D_MATRIX_REMOVED_ONE);}
Run Code Online (Sandbox Code Playgroud)
removeSpecifiedElementInArray()
从具有指定值的数组中删除元素并返回数组.此测试仅在写入expilicitly时才有效.所以在这个例子中只有第二个有效.
assertArrayEquals(ArrayUtilities.removeSpecifiedElementInArray(ONE_D_MATRIX, 7), ONE_D_MATRIX_REMOVED_ONE);
assertArrayEquals(ArrayUtilities.removeSpecifiedElementInArray(new int[] { 12, 34, 45, 6, 7, 85, 67, 43 }, 7), new int[] {12, 34, 45, 6, 85, 67, 43});
Run Code Online (Sandbox Code Playgroud)
JUnit说:
java.lang.AssertionError:数组长度不同,expected.length = 8 actual.length = 7
这是一个错误吗?第一种方法为什么第二种方法不起作用呢?