CodeGym /课程 /Java 语法 /针对数组的基本操作

针对数组的基本操作

Java 语法
第 7 级 , 课程 3
可用

“以下是几个有趣的数组操作示例:”

代码示例 1。
用从 1 到 10 的数字填充 10 元素数组:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i + 1;
        }
    }
}
用从 10 到 1 的数字填充 10 元素数组:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 10 - i;
        }
    }
}
用从 0 到 9 的数字填充 10 元素数组:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i;
        }
    }
}
用从 9 到 0 的数字填充 10 元素数组:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 9 - i;
        }
    }
}
示例 2。
从键盘读取 10 个字符串:
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String[] list = new String[10];

    for (int i = 0; i < list.length; i++)
    {
      list[i] = reader.readLine();
     }
  }
}
从键盘读取 10 个数字:
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] list = new int[10];

    for (int i = 0; i < list.length; i++)
    {
      String s = reader.readLine();
      list[i] = Integer.parseInt(s);
    }
  }
}
示例 3。
在屏幕上显示数组:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = new int[10];

        // 填充数组
        for (int i = 0; i < list.length; i++) 
           list[i] = i;

        // 显示数组内容
        for (int i = 0; i < list.length; i++)  
          System.out.println(list[i]);
    }
}
示例 4。
快速 (static) 初始化。将数组元素相加:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        // static 初始化
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};        
        
        // 计算总和
        int sum = 0;
        for (int i = 0; i < list.length; i++) 
           sum += list[i];

        System.out.println("总和为 " + sum);
    }
}
示例 5。
找出数组中的最小元素:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};

        int min = list[0];
        for (int i = 1; i < list.length; i++)
        {
             if (list[i] < min) 
                  min = list[i];
        }

       System.out.println ("最小值为 " + min);
    }
}
评论 (20)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Anonymous #11681054 级别 1,Zurich,Switzerland
22 九月 2025
打卡
7 二月 2025
// 静态初始化 int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0}; // int[] numbers = new int[10]; // 动态初始化 for (int i = 0; i < numbers.length; i++) { numbers[i] = i; } 第一种方式被称为静态初始化是因为在编译时就已经确定了数组的值。
Gould 级别 10,Guangzhou,China
11 七月 2024
在Java中,数组的索引总是从0开始到数组长度减1。如果数组长度为10,那么有效的索引范围是从0到9。尝试访问索引10或更高索引的元素将会导致ArrayIndexOutOfBoundsException,因为这些索引超出了数组的有效范围。
冲击 级别 2,Seattle,China
4 十二月 2023
现在七级 打卡留恋
Qin-1999 级别 22
29 十二月 2023
ohhhhhh,现在15级了
简默 级别 15,Central District,Hong Kong
23 十一月 2023
开通会员的第17天👀
黄z聰 级别 18,Tokyo,China
9 十一月 2023
打卡,后面还有人吗
Anonymous #11374458 级别 16,Cambridge,中国
10 八月 2023
🤪加油 ! 坚持下去
1 十月 2022
打卡
Anonymous #10922321 级别 7,Hangzhou,China
2 三月 2022
求问循环里的这句 numbers[i] = i;都代表什么
15 三月 2022
给数组里的数字赋值,number[0]=0
小白一枚 级别 8,Changsha
4 八月 2021
就这
龚凌江 级别 13,China,China
19 十二月 2021
就这你也放弃了;
Bob 级别 20,伯明翰,美国
29 六月 2022
别放弃啊
级别 8,Guangzhou,China
12 七月 2022
共勉 🤝
元. 级别 26,Taipei,Taiwan, Province of China
2 一月 2023
加油 別放棄