java 中 String 数组怎么动态赋值

如题所述

首先明确一下数组的初始化:

//静态初始化
int a[]={1,2,3};
//动态初始化
int a[]; a = new int[3] ; a[0] = 1;a[1] = 2;a[2]=3;
//默认初始化
int a [] = new int [5] ;

JAVA是强类型,数组类型一旦声明,就不能更改了;

如果数组长度确定,我想所谓的“动态赋值”用循环应该可以搞定;

如果数组的长度是不确定的,我认为你这个提问才有点价值,因为这需要根据具体情况来改变数组的长度,告诉你java是怎么干的,用的 System.arraycopy,  jdk 里好多操作数组的源码都是用的这个,比如 ArrayList的实现


闲着没事写了个小例子玩玩:


import java.util.Random;
import java.util.Scanner;
/**
 *
 * @author LYTG
 * @since 2015-12-29 上午12:21:09
 */
public class DemoTest {
    /**
    * @author LYTG
    * @param args
    */
    public static void main(String[] args) {
        test2();
    }
    /**
    * @author LYTG
    * @param array
    */
    public static void out(String[] array){
        if(array==null){
            System.out.println("null");
            return;
        }
        if(array.length<1){
            System.out.println("{ }");
            return;
        }
        StringBuffer sb = new StringBuffer();
        sb.append("{\"");
        sb.append(array[0]);
        sb.append("\"");
        for(int i = 1; i<array.length; i++){
            sb.append(",\"");
            sb.append(array[i]);
            sb.append("\"");
        }
        sb.append("}");
        System.out.println(sb.toString());
    }
    /**
    * 声明一个长度为5的String类型数组,并为每个数组元素赋值一个随机整数
    * @author LYTG
    */
    public static void test1(){
        String[] array = new String[5];
        for(int i = 0; i<array.length; i++){
            array[i] = String.valueOf(new Random().nextInt());
        }
        out(array);
    }
    /**
    * 在控制台输入字符,把每次输入的字符保存到一个数组中,
    * 如果输入的字符串中包含空格,则切割后再分别保存到此数组中,
    * 在每次输入完成后输出整个数组,直到手动终止程序
    * @author LYTG
    */
    private static String[] array = new String[0];
    public static void test2(){
        Scanner sc = new  Scanner(System.in);
        System.out.println("请输入一串字符(输入完请敲回车):");
        String input = sc.nextLine();
        if(input.contains(" ")){
            String[] a = input.split(" ");
            int arrayLength = array.length;
            int aLength = a.length;
            String[] newArray = new String[arrayLength + aLength];
            System.arraycopy(array, 0, newArray, 0, arrayLength);
            System.arraycopy(a, 0, newArray, arrayLength, aLength);
            array = newArray;
        }else{
            int arrayLength = array.length;
            String[] newArray = new String[arrayLength + 1];
            System.arraycopy(array, 0, newArray, 0, arrayLength);
            newArray[arrayLength] = input;
            array = newArray;
        }
        out(array);
        test2();
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-29
如果要用动态数组的话,建议使用JDK自带的集合类 List List是个泛型集合可放存放任意类型的数据。List<String> list = new ArrayList<String>();即可。ArrayList就是数组方式实现的。本回答被网友采纳
第2个回答  2015-12-29
写个方法传参数,或者写在构造函数里
第3个回答  2016-04-15
一维数组的声明方式:
type var[]; 或type[] var;
声明数组时不能指定其长度(数组中元素的个数),
Java中使用关键字new创建数组对象,格式为:
数组名 = new 数组元素的类型 [数组元素的个数]
实例:
TestNew.java:
程序代码:
public class TestNew { public static void main(String args[]) { int[] s ; int i ; s = new int[5] ; for(i = 0 ; i 5 ; i++) { s[i] = i ; } for(i = 4 ; i >= 0 ; i--) { System.out.println("" + s[i]) ; } } }

初始化:
1.动态初始化:数组定义与为数组分配空间和赋值的操作分开进行;
2.静态初始化:在定义数字的同时就为数组元素分配空间并赋值;
3.默认初始化:数组是引用类型,它的元素相当于类的成员变量,因此数组分配空间后,每个元素也被按照成员变量的规则被隐士初始化。
实例:

TestD.java(动态):
程序代码:
public class TestD { public static void main(String args[]) { int a[] ; a = new int[3] ; a[0] = 0 ; a[1] = 1 ; a[2] = 2 ; Date days[] ; days = new Date[3] ; days[0] = new Date(2008,4,5) ; days[1] = new Date(2008,2,31) ; days[2] = new Date(2008,4,4) ; } } class Date { int year,month,day ; Date(int year ,int month ,int day) { this.year = year ; this.month = month ; this.day = day ; } }

TestS.java(静态):
程序代码:
public class TestS { public static void main(String args[]) { int a[] = {0,1,2} ; Time times [] = {new Time(19,42,42),new Time(1,23,54),new Time(5,3,2)} ; } } class Time { int hour,min,sec ; Time(int hour ,int min ,int sec) { this.hour = hour ; this.min = min ; this.sec = sec ; } }

TestDefault.java(默认):
程序代码:
public class TestDefault { public static void main(String args[]) { int a [] = new int [5] ; System.out.println("" + a[3]) ; } }

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网