① java中如何創建字元串數組
java中定義一個字元串數組方式如下:
1.String[] str={"AAA","BBB","CCC"};
2.String str[]={"AAA","BBB","CCC"};
string類型和其他基本類型相似,創建數組時,有上述兩種方式。
數組可以分為一維數組和二維數組;
一維數組的語法格式:
數組元素類型 數組名[ ]={數組元素的初值,。。。}
如: int sa[]={1,2,3}
二維數組聲明的語法格式:
數組元素類型 數組名[ ][ ]
如:int a[][]=new int [2[3]
都是一個原理的,自己換一下自己想要定義的數組類型就可以了。
字元串數組的創建:
String a =new String("Java");
不過推薦用ArrayList strArray = new ArrayList (); 比較靈活。
② java中怎麼創建對象數組
//如果你是初學JAVA,建議你看看這段代碼,裡面有一些基本技巧
//有疑問可以問我!
import java.io.*;
public class StudentInformation {
public static void main(String args[]) throws IOException {
Student[] st = new Student[5];// 第一個錯誤
int i = 0;
st[i++] = new Student().setStudent("WangZhen", 20, 1, 1.76);
st[i++] = new Student().setStudent("YangZhen", 21, 2, 1.66);
st[i++] = new Student().setStudent("Wangqiang", 19, 3, 1.86);
st[i++] = new Student().setStudent("XiaoMing", 18, 4, 1.71);
st[i++] = new Student().setStudent("XiaoHong", 22, 5, 1.74);
for (i = 0; i < st.length; i++)
st[i].display();
System.out.println("請輸入要查詢學生的姓名");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (i = 0; i < st.length; i++) {
if (st[i].name.equals(str)) {
System.out.println("所要查找學生為");
st[i].display();
}
}
}
}
class Student {
String name;
int age;
int num;// 學號
double high;// 身高
public void display() {
System.out.println("姓名:" + name + "\t年齡:" + age + "\t身高:" + high + "\t學號:" + num);
}
public Student setStudent(String n, int a, int nu, double h) {
name = n;
age = a;
num = nu;
high = h;
return this;
}
}
③ java 數組創建問題
動態初始化:數組定義與為數組分配空間和賦值的操作分開進行
如:
int intArray[];
聲明了一個整型數組,數組中的每個元素為整型數據。與C、C++不同,Java在數組的定義中並不為數組元素分配內存,因此[]中不用指出數組中元素個數,即數組長度,而且對於如上定義的一個數組是不能訪問它的任何元素的。我們必須為它分配內存空間,這時要用到運算符new,其格式如下:
arrayName=new type[arraySize];
其中,arraySize指明數組的長度。如:
intArray=new int[3];
為一個整型數組分配3個int型整數所佔據的內存空間。
通常,這兩部分可以合在一起,格式如下:
type arrayName=new type[arraySize];
例如:
int intArray=new int[3];
④ 如何創建JAVA數組文件
創建一個file
然後用流對象 給每一行寫入數組裡面的內容
關閉流
⑤ java中創建String數組的問題
1、定義字元串數組時錯誤,應該為:
String[] aclass={"1","2","3","4","5"};
2、調用方法時沒有傳入參數,應該為
if(n==1){
ShuName(aclass);
}else if(n==2){
PaiName(aclass);
}else{
DianName(aclass);
}
⑥ 怎樣用java定義一個int數組
1、首先在java軟體中,在項目中引入hutool的jar包,如下圖所示。
⑦ java中怎麼創建對象數組
首先我們需要創建一個class:
classStudent{
Stringname;
doublescore;
Stringnum;
Student(Stringn,doubles,Stringm){
name=n;
s=score;
num=m;
}
publicstaticvoidprintInfo(){
System.out.println(num+","+name+","+score);
}
}
接下來我們對此類進行數組的創建:
//1
Studentstu[];<spanstyle="white-space:pre"></span>//聲明數組。
stu=newStudent[3];<spanstyle="white-space:pre"></span>//創建數組,這里是創建的一個引用的數組,每一個引用並沒有確切的地址。
for(inti=0;i<3;i++){<spanstyle="white-space:pre"></span>//為數組創建對象,也就是說為創建的引用關聯到確切的地址。
stu[i]=newStudent();
}
//2
Studentstu[]=newStudent[3];
for(inti=0;i<3;i++){
stu[i]=newStudent();
}
//3
Studentstu[]=newStudent{newStudent(sjl,87,01),newStudent(ljs,98,02),newStudent(lls,92,03)};
⑧ java 數組的定義與創建
Stringinfo[];
info=newString[2];
錯了
你是自創的java代碼吧
1.Stringinfo[]=newString[2];
2.Stringinfo[]={};
⑨ 用java基礎編寫程序,創建一個數組並賦值
代碼:
publicclassArrayTest{
//聲明數組
long[]ii={8,4,2,1,23,344,12};
//判斷
publicvoidjudge(longlon){
for(inti=0;i<ii.length;i++){
if(lon==ii[i]){
System.out.println("猜對了");
return;
}
}
System.out.println("Sorry!");
}
//測試
publicstaticvoidmain(String[]args){
ArrayTestarraytest=newArrayTest();
//手動輸入整數
Scannerscanner=newScanner(System.in);
System.out.println("請輸入一個整數:");
longl=scanner.nextLong();
arraytest.judge(l);
}
}
運行結果:
⑩ java的數組和創建
在JAVA中創建數組有兩種方式 (1)靜態創建 如String[] array = {"a", "b", "c"}; (2)動態創建 如String[] array = new String[] {"a", "b", "c"};或String[] array = new String[3];(先確認元素個數) 一般情況下習慣使用動態創建方式 比較靈活 可以先規定元素個數 後對每個元素進行賦值