java - setting the values of arrays in java by using their indexes
问题描述
How do you initialized an array in java first, and then set values to them by using their indexes? So for example, you make an array in java, and then you want the value of the number 75 index of the array to be set to "seventy five", can you do something like array[75] = "seventy five"?;
String[] array;
array[0] = "zero";
array[1] = "one";
array[2] = "two";
When I tried the codes below it says unknown class array. What am I doing wrong?
String[] array = new String[10];
array[0] = "zero";
解决方案
First, you'll need to point the array reference to an actual array object.
For example,
String[] array = new String[3];
You can initialize the contents like you're doing.
Or you can initialize them in the array creation expression:
String[] array = new String[] { "zero", "one", "two" };
You can also the array initializer by itself in the declaration:
String[] array = { "zero", "one", "two" };
推荐阅读
- python - 检查函数是否在 Python 类中定义
- php - 无法使用 Html2pdf/TCPDF (PHP) 添加字体
- node.js - 在 Zapier 的 NodeJS 模块中发布表单数据
- assembly - MIPS lw 指令到底是做什么的?
- json - Insights-Widget“何时”条件值选项
- sql - 使用给定的频率产生额外的记录
- loops - 是否可以仅使用 for 循环来实现包含 for 循环的递归函数,该循环包含对上述函数的调用?
- android - Android软导航在菜单点击的内容前面
- python - Pandas 删除换行符并将文本文件转换为 CSV
- r - 增加ggplot2中图例键之间的垂直间距