为什么输出与此Java代码一样?

时间:2016-05-19 10:56:07

标签: java arrays matrix

以下代码:

public class StrangeClass {
    public static int[] alpha(int[] a) {
        int[] b = new int[a.length];
        for (int i = 0; i < a.length; i++)
            b[i] = a[a.length - 1 - i];
        return b;
    }

    public static void omega(int[] a) {
        for (int i = 0; i < a.length/2; i++) {
            int temp = a[i];
            a[i] = a[a.length - 1 - i];
            a[a.length - 1 - i] = temp;
        }
    }

    public static void main(String[] args) {
        int[][] matrix = { {1,2,3}, {4,5,6} };
        System.out.println(matrix.length);
        alpha(matrix[0]);
        omega(matrix[1]);
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++)
                System.out.print(matrix[i][j]);
            System.out.println();
        }

产生输出:

2
123
654

我想知道如何达到输出。这里的输出是正确的,我只是不明白它如何得到三行作为答案的概念。

4 个答案:

答案 0 :(得分:0)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Cannot create inner bean '(inner bean)' of type [org.springframework.beans.factory.config.PropertiesFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#15': Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [users.properties] cannot be opened because it does not exist at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:279) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:126) at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:631) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:145) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1075) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:979) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:320) ... 118 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#15': Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [users.properties] cannot be opened because it does not exist at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:269) ... 130 more Caused by: java.io.FileNotFoundException: class path resource [users.properties] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157) at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143) at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175) at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156) at org.springframework.beans.factory.config.PropertiesFactoryBean.createInstance(PropertiesFactoryBean.java:113) at org.springframework.beans.factory.config.PropertiesFactoryBean.createProperties(PropertiesFactoryBean.java:98) at org.springframework.beans.factory.config.PropertiesFactoryBean.afterPropertiesSet(PropertiesFactoryBean.java:69) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509) ... 133 more 方法返回一个反向排序的数组,而alpha方法直接反转参数中传递的数组。 因此,当omega更改矩阵第二行的顺序时,alpha(matrix[0]);无法看到任何内容。 正如Shree Krishna所说,你可以通过调试找到它。

答案 1 :(得分:0)

    public class StrangeClass {
        public static int[] alpha(int[] a) {
            int[] b = new int[a.length];
            for (int i = 0; i < a.length; i++)
                b[i] = a[a.length - 1 - i];
            return b;
        }

        public static void omega(int[] a) {
            for (int i = 0; i < a.length / 2; i++) {
                int temp = a[i];
                a[i] = a[a.length - 1 - i];
                a[a.length - 1 - i] = temp;
            }
        }

        public static void main(String[] args) {
            int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };
            System.out.println(matrix.length); //Line a
            alpha(matrix[0]); //Line b
            omega(matrix[1]); //Line c
            for (int i = 0; i < 2; i++) { //Line d
                for (int j = 0; j < 3; j++)
                    System.out.print(matrix[i][j]);
                System.out.println();
            }
        }
    }

行a打印矩阵的长度,即两个数组元素{ 1, 2, 3 }{ 4, 5, 6 }

第b行通过引用传递{ 1, 2, 3 },但alpha中的更改将传递给另一个数组b。因此数组{ 1, 2, 3 }保持不变。

第c行通过引用传递{ 4, 5, 6 },并对相同的数组进行更改。因为它通过引用传递(默认情况下在java中),所以值被反转(alpha和omega是数组的反转值)

第d行负责打印其中{ 1, 2, 3 }保持不变的矩阵数组,该数组传递给传递给omega的alpha和{ 4, 5, 6 }更改。

答案 2 :(得分:0)

1)matrix是一个包含2个数组的数组,每个数组都有3个int值

2)方法alpha返回一个新的int值数组,其值以相反的顺序作为输入数组

3)方法omega反转输入数组的值

答案 3 :(得分:0)

数组作为参考传递。

omega反转数组,因为操作发生在数组a []本身(作为参数传递的数组),而方法alpha在b [](而不是传递的数组)中创建了反向数组并返回它。