首页 > 代码库 > JAVA之数组
JAVA之数组
/**
* 功能:数组的定义
*/
package com.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class test6 {
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//简单数组
int[] xx = new int[5];
int[] yy = {1,2,3,4,5,6,7,8,9};
int[] zz = {};
System.out.println(xx[4]);
System.out.println(xx.length);
System.out.println(yy[4]);
System.out.println(zz.length);
//对象数组
Ox[] ox = new Ox[4];
//给每头牛赋初值
//建立输入通道
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
//从控制台读取数据
for(int i=0; i<ox.length; i++)
{
ox[i] = new Ox();
System.out.println("输入牛牛的名字:");
ox[i].setName(String.valueOf(br.readLine()));
System.out.println("输入牛牛的体重:");
ox[i].setWeight(Integer.valueOf(br.readLine()));
}
//打印每头牛的信息
for(int j=0; j<ox.length; j++)
{
System.out.println(ox[j].getName());
System.out.println(ox[j].getWeight());
}
}
}
//定义一个牛类
class Ox
{
private String name;
private int weight ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
小结:
JAVA之数组