當前位置:首頁 » 編程語言 » java創建數組

java創建數組

發布時間: 2022-01-09 15:47:35

java怎樣定義一個創建數組的方法

importjava.util.ArrayList;
importjava.util.List;
publicclassClientSocket{
publicstaticvoidmain(String[]args)throwsException{
List<Object>list=newArrayList<Object>();//這里類型你自己指定
list.add("asd");
list.add(123);
Object[]obj=newObject[list.size()];
obj=list.toArray(obj);
}
}

原理:ArrayList底層本身就是一個可變長度的數組,用ArrayList更方便,不用擔心溢出。

② 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的數組和創建

在JAVA中創建數組有兩種方式 (1)靜態創建 如String[] array = {"a", "b", "c"}; (2)動態創建 如String[] array = new String[] {"a", "b", "c"};或String[] array = new String[3];(先確認元素個數) 一般情況下習慣使用動態創建方式 比較靈活 可以先規定元素個數 後對每個元素進行賦值

④ 求java創建數組代碼

package test;

import java.util.Random;

public class Test {
public static void main(String[] args) {
int maxVal=-1;
int maxRow=-1;
int maxCol=-1;
int[][] arr=new int[5][5] ;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){

arr[i][j]=new Random().nextInt(100);
if(maxVal<arr[i][j]){
maxVal=arr[i][j];
maxRow=i;
maxCol=j;
}
System.out.print(arr[i][j]+" ");

}
System.out.println();

}
System.out.println("最大行 "+maxRow);

System.out.println("最大列 "+maxCol);
System.out.println("max number is "+maxVal);

}
}

⑤ java怎麼創建一個數組,每個元素是一個數組

用 ArrayList 里 放數組吧,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數組文件

創建一個file
然後用流對象 給每一行寫入數組裡面的內容
關閉流

⑧ java創建3維的數組

public class SearchArray {
public static void main(String[] args) {
String[][] arrayOfSring =
new String[][] { {"abc", "de", "gong"}, {"De", "abc"}, {"Foo", "gong"},
{"hello", "java", "world"}};
String target = "abc";
endPoint: for (int i = 0; i < arrayOfSring.length; i++) {
for (int j = 0; j < arrayOfSring[i].length; j++) {
String arrayElement = arrayOfSring[i][j];
if (arrayElement.equals(target)) {
System.out.println(target + "第一次出現在arrayOfSring數組中的位置:");
System.out.println(target + "arrayOfSring[" + i + "][" + j + "]");
break endPoint;
}
}
}
}
}

⑨ 怎樣用java定義一個int數組

int[] anArray = null;/*** 這樣賦值在java中是不允許的,會判定為語法錯誤
* 首先java只可以在class中【聲明】成員變數,而不能對變數賦值,
* 你只能在方法中(例如:main方法或者其他方法)對變數賦值。** 但是class中允許在聲明變數的同時,為變數賦【初始值】。
* 也就是說 int[] anArray2 = new int[10]; 這樣是允許的
* 這里的 new int[10] 是初始化操作(不是賦值操作)。
* *///anArray = new int[10];
static int[] bArray = null;
public void setValue(){
anArray = new int[10]; //這里是可以的,方法內可以賦值。}public static void main(String[] args){
//anArray = new int[10];/*** 這里是不允許的,因為主方法(即main方法)是靜態的(由static修飾)
* 所以如果是靜態的成員變數可以賦值,例如 bArray
* 下面的兩種方式都是可以賦值的* */bArray = new int[10];
bArray = new int[]{1,2,2,1,1,1,1,1,1,1};}}<b答案補充</b
static修飾的方法,只可以調用static修飾的成員變數
所以在main方法中為【非靜態】的anArray數組賦值是不允許的。
另外bArray = new int[]{1,2,2,1,1,1,1,1,1,1};是為數組賦值的方式。

⑩ java 數組的定義與創建

Stringinfo[];
info=newString[2];
錯了
你是自創的java代碼吧


1.Stringinfo[]=newString[2];
2.Stringinfo[]={};

熱點內容
顯卡怎麼保存配置 發布:2024-04-20 09:28:52 瀏覽:596
校園交易網站源碼 發布:2024-04-20 09:18:54 瀏覽:701
江蘇北斗授時伺服器ip雲空間 發布:2024-04-20 08:53:50 瀏覽:931
dedecms批量上傳圖片 發布:2024-04-20 08:42:11 瀏覽:966
酷q如何編譯 發布:2024-04-20 08:41:27 瀏覽:79
安卓手機數字人民幣怎麼下載 發布:2024-04-20 08:38:21 瀏覽:114
access如何配置資料庫 發布:2024-04-20 08:37:35 瀏覽:504
手寫輸入演算法 發布:2024-04-20 08:29:31 瀏覽:258
朝夕源碼 發布:2024-04-20 08:24:15 瀏覽:276
minilinux 發布:2024-04-20 08:23:38 瀏覽:50