Java 中初始化数组有以下 3 种方式。
使用 new 指定数组大小后进行初始化
使用 new 指定数组元素的值
直接指定数组元素的值
// 使用 new 指定数组大小后进行初始化
int[] number = new int[5];
number[0] = 1;
number[1] = 2;
number[2] = 3;
number[3] = 5;
number[4] = 8;
// 使用 new 指定数组元素的值(2种方式)
int[] number = new int[] { 1, 2, 3, 5, 8 };
int[] number = new int[5] { 1, 2, 3, 5, 8 };
// 直接指定数组元素的值(2种方式)
int[] number = { 1, 2, 3, 5, 8 };
int[] number;
number={1,2,3,5,8};
Java求数组元素的最大和最小值
public class Example{
public static void main(String[] args) {
int score[] = { 67, 89, 87, 69, 90, 100, 75, 90 }; // 初始化数组
int max = 0; // 定义变量保存到最大值
int min = 0; // 定义变量保存到最小值
max = min = score[0]; // 把第1个元素的内容赋值给max和min
for (int x = 0; x < score.length; x++) { // 循环求岀最大和最小
if (score[x] > max) { // 依次判断后续元素是否比max大
max = score[x]; // 如果大则修改max内容
}
if (score[x] < min) { // 依次判断后续的元素是否比min小
min = score[x]; // 如果小,则修改min内容
}
}
System.out.println("最高成绩:" + max); // 输出最大值
System.out.println("最低成绩:" + min); // 输出最小值
}
}
在一个货架上有 5 件商品,编写程序,在输入商品价格之后输出最高价格、总价格和平均价格。
首先要创建一个包含 5 个空元素的价格数组,然后使用 for 循环使用户从控制台录入商品的价格,并将价格保存至数组中,再使用一个 for 循环来遍历该数组,求出最高价格和总价格。最后用总价格除以商品数量算出平均价格。
public static void main(String[] args)
{
// 声明数组
int[] prices = new int[5];
int maxPrice = 0, avgPrice = 0, sumPrice = 0;
Scanner input = new Scanner(System.in);
System.out.println("请输入5件商品的价格:");
for (int i = 0; i < 5; i++)
{
prices[i] = input.nextInt(); // 循环向数组中元素赋值
}
// 计算价格最大值
maxPrice = prices[0]; // 假设最大值为第一个元素
for (int index = 1; index < prices.length; index++)
{
sumPrice += prices[index]; // 汇总价格
if (prices[index] > maxPrice)
{
maxPrice = prices[index];
}
}
// 平均价格=总价格/商品数量
avgPrice = sumPrice / prices.length;
System.out.println("本货架上商品的总价格为:" + sumPrice + " 平均价格为:" + avgPrice + " 最高价格为:" + maxPrice);
}
综合一维数组和二维数组的相关知识,以及数组排序的多种算法来实现商品信息查询的功能。
假设在仓库系统中,每件商品都有 3 个库存信息,分别是入库量、出库量和当前库存量。定义一个一维数组来存储 5 件商品的名称,并定义一个二维数组来存储这 5 件商品的 3 个库存信息。用户可以根据商品名称查询该商品的所有库存,也可以查看某个类别库存下数量小于 100 的商品名单,并将该类别的所有库存量按从低到高的顺序排列。
import java.util.Scanner;
public class Test28 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String[] products = { "洗发水", "纸巾", "水杯", "牙膏", "香皂" };
int[][] amounts = { { 50, 80, 90 }, { 40, 80, 78 }, { 50, 45, 789 }, { 100, 685, 55 }, { 898, 754, 63 },
{ 99, 478, 685 } };
System.out.println("*************** 库存系统 ***************");
System.out.println("请输入要查询库存信息的商品名称:");
String name = input.next();
for (int i = 0; i < products.length; i++) {
if (products[i].equals(name)) {
System.out.println("商品【" + products[i] + "】的库存信息如下:");
System.out.println("入库 \t 出库 \t 库存");
for (int j = 0; j < 3; j++) {
System.out.print(amounts[i][j] + "\t");
}
break;
}
}
System.out.println("\n*************** 查询库存不足 100 的商品 ***************");
System.out.println("1.入库 \t2.出库 \t3.库存");
System.out.println("请输入序号:");
int no = input.nextInt();
int[] temp = new int[5]; // 定义数组,存储该类别的所有商品
System.out.println("该类别下数量较少的商品有:");
for (int i = 0; i < 5; i++) {
temp[i] = amounts[i][no - 1]; // 将指定类别的所有商品名称存储到temp数组中
if (amounts[i][no - 1] < 60) {
System.out.print(products[i] + "\t");
}
}
// 使用冒泡排序,将商品的库存量以从低到高的顺序排列
for (int i = 1; i < temp.length; i++) {
for (int j = 0; j < temp.length - i; j++) {
if (temp[j] > temp[j + 1]) {
int x = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = x;
}
}
}
System.out.println("\n该类别的商品库存信息从低到高的排列如下:");
for (int i = 0; i < temp.length; i++) {
System.out.print(temp[i] + "\t");
}
}
}