java - 如何将 int[] 数组传递到 main(String []args) 中?

标签 java arrays

我正在尝试编写一个从 main 方法开始的程序,并调用一个包含整数数组的方法。

本质上,该程序是一个基本的提升,假设所有输入都是同时接收的。电梯将进行的停靠点存储在名为 stoppages 的数组中,该数组是电梯上行停靠点和电梯下行停靠点的组合。

我希望程序在 main 中启动,并调用对所有楼层等进行排序的方法。但是,当我运行该程序时,它会将最终数组的所有值更改为 0。

为什么要这样做,需要做什么才能让数组值在程序运行时保持不变?

这是主要方法:

public class user_interface {

public static void main(String[] args) {
    floor_sorting.sorting_floors(args);
     }
}

这是我尝试调用的方法:

import java.util.*;
 public class floor_sorting{

   public static void sorting_floors(int[] args){

      //bubble sort - sorts out user_up in ascending order
      for (int i = 0; i < user_stops.user_up.length; i++) {
            for (int x = 1; x < user_stops.user_up.length - i; x++) {
                   if (user_stops.user_up[x-1] > user_stops.user_up[x]){
                         int temp = user_stops.user_up[x-1];
                         user_stops.user_up[x-1] = user_stops.user_up[x];
                         user_stops.user_up[x] = temp;   
                   }
            }
      }

      //bubble sort again, but in descending order
      for (int i = 0; i < user_stops.user_down.length; i++) {
            for (int x = 1; x < user_stops.user_down.length - i; x++) {
              if (user_stops.user_down[x-1] < user_stops.user_down[x]){
                     int temp = user_stops.user_down[x-1];
                     user_stops.user_down[x-1] = user_stops.user_down[x];
                     user_stops.user_down[x] = temp;       
              }
      }
      }


      //merges the two separate arrays into one (user_up + user_down = stoppages)  
      int stoppages[] = new int[user_stops.user_up.length+user_stops.user_down.length];
      System.arraycopy(user_stops.user_up, 0, stoppages,0, user_stops.user_up.length);
      System.arraycopy(user_stops.user_down, 0, stoppages, user_stops.user_up.length, user_stops.user_down.length);

      int c = 0;

      do {
            System.out.println("The lift has come to stop at floor " + stoppages[c]);
          System.out.println("The lift has started moving again."); 
          c++;
      } while (c < stoppages.length);

      do {
            System.out.println("This lift has stopped moving forever.");
      } while (c!=stoppages.length);
   }
   }

上面的方法调用了这个方法:

import java.util.*;
public class user_stops {
//user_up array declaration 
public static int user_up[] = new int [6];{

//Initialising data in array     
user_up[0] = 1;
user_up[1] = 3;

user_up[2] = 2;
user_up[3] = 7;

user_up[4] = 5;
user_up[5] = 8;
}


//user_down array declaration   
public static int user_down[] = new int [6];{

//user_down data initialisation
user_down[0] = 8;
user_down[1] = 5;

user_down[2] = 4;
user_down[3] = 2;

user_down[4] = 6;
user_down[5] = 1;

}

}

最佳答案

I want the program to start in main, and call the method that sorts all of the floors and such. However, when I run the program it changes all of the values of the final array into 0's.

您向我们展示的代码甚至无法编译。此行无效:

floor_sorting.sorting_floors;

如果您不向我们展示真实的代码,我们就无法解释程序的作用。

<小时/>

如果你想让它编译,你需要将 String 数组转换为 int 数组。简单的方法是:

  1. 分配一个正确大小的 int[](即与 String[] args 数组大小相同),
  2. 遍历数组的范围,并且
  3. 使用Integer.parseInt(String)转换值。

阅读 Integer 类的 javadoc ...

<小时/>

最后,您需要更加注意风格:

  • 您的代码缩进一团糟。
  • 您违反了标识符的样式规则。类名应以大写字母开头,并且应使用“驼峰式大小写”而不是下划线。

像上面这样的代码很难阅读,并且会让你受到同事无休止的批评/提示。 (如果这是一个已标记的作业,它应该会让你失去分数。)

关于java - 如何将 int[] 数组传递到 main(String []args) 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21407966/

相关文章:

C 数组作为函数参数 : Size check?

c++ - C/C++ - 在不使用内置函数的情况下旋转数组的有效方法(作业)

java - 使用 BigInteger 实现的 RSA 算法没有得到正确的输出

java - 从中间代码到Java字节码(龙书)

java - 在 Android 应用程序中使用属性文件时出现 NullPointerException

c++ - 如何将字符串变量与从文件创建的字符串数组进行比较

c# - 找出所有相同的答案

java - 为什么在我的字符串数组中的字符串之前打印 null?

java - 使用 ImageJ 库 (Java) 增加图像对比度

java - 将值动态附加到 long[] 数组中