導航:首頁 > 編程語言 > java字元串存入數組

java字元串存入數組

發布時間:2022-06-19 13:11:49

『壹』 java中怎樣把字元存到數組中

java將字元存到數組中,可以使用scanner類接受用戶從鍵盤輸入的字元,然後通過for循環語句,放入數組中,如下代碼:

importjava.util.Scanner;
publicclassc12{
publicstaticvoidmain(String[]args){
Scanners=newScanner(System.in);
System.out.println("請輸入100個字母");//輸入的字母數量
String[]a=newString[100];
for(inti=0;i<=100;i++)
a[i]=s.next();//放入數組中
for(inti=0;i<=100;i++){
System.out.print(a[i]+" ");
}
}
}

『貳』 字元串放入數組,Java的。

String a = "abc";
String[] arr = new String[a.length()];
for(int i = 0; i <arr.length; i++){
arr[i] = a.substring(i, (i+1));
}
//最後輸出數組就可以了。
System.out.println(java.util.Arrays.toString[arr]);

『叄』 java中如何將一個字元串賦給一個數組

賦給字元數組:char[] chars = s.toCharArray();

因為這里是位元組,所以直接列印出來顯示不是abcd,先要轉換為char類型的再列印

『肆』 JAVA中怎樣把用戶輸入的字元串存入數組中

import java.util.Scanner;

import java.util.InputMismatchException;

public class saveInputToArr {

public static void main(String[] args) {

Scanner scan = null;

try {

scan = new Scanner(System.in);

System.out.print( "請輸入個數: " );

int inputNum = scan.nextInt();

if( inputNum <= 0 ) {

throw new Exception( "輸入有誤" );

}

System.out.println( "請輸入數字: " );

int arr[] = new int[inputNum];

int num = 0;

int count = 0;

while( count < inputNum ) {

num = scan.nextInt();

arr[count] = num;

count++;

}

for( int i = 0; i < arr.length; i++ ) {

System.out.print( arr[i] + " " );

}

} catch ( Exception e ) {

throw new InputMismatchException( "" );

} finally {

try {

if ( scan != null ) {

scan.close();

}

} catch ( Exception e2 ) {

e2.printStackTrace();

}

}

}

}

運行結果為:

請輸入個數:2

請輸入數字:99

123

99 123

(4)java字元串存入數組擴展閱讀

Java從輸入中讀取一個數組

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

String str = sc.nextLine().toString();//用nextLine()可以讀取一整行,包括了空格,next()卻不能讀取空格

String arr[] = str.split(" ");//拆分字元串成字元串數組

int a[] = new int[arr.length];

for(int j = 0; j < a.length; j++)

{

a[j] = Integer.parseInt(arr[j]);

System.out.print(a[j] + " ");

}

}

}

『伍』 用java將字元串存入數組

一行存入一個數組嗎?

String[]array;
stringstr;
inti;
FileReaderword=newFileReader("word.txt");
BufferedReaderbr=newBufferedReader(word);
while((str=br.readLine())!=null){
array[i]=str;
i++;
}

『陸』 java 如何把string 加入數組中

可以使用split函數將String 字元串轉化為數組

split 方法
將一個字元串分割為子字元串,然後將結果作為字元串數組返回。

例子:

String[]arr1="String".split("");
for(inti=0;i<arr1.length;i++){
System.out.println(arr1[i]);
}

結果:

S

t

r

i

n

g

『柒』 java怎麼從一個文件讀取字元串,再存到一個字元串數組里

首先,可以直接寫入string的,這段程序的這種寫法很無聊,讓你誤解了。
如: out.write(p_send_text);

其次,如果想寫入一行並且換行的話,那麼得包裝一個printwriter,如:
PrintWriter out = new PrintWriter(FileWriter(file, true));
out.println(p_send_text);

在Java里,
char表示一個字元,它可以直接轉換為int, byte, long.(ascii/unicode碼)
String表示一串字元,它可以通過某些方法轉換成一個數組,如char[], byte[],也可以用其他方法取出其中某個特定位置的字元,如charAt();

與C裡面不同,在Java中,通常String用的比較多,char[]基本不用的。

『捌』 Java中如何實現用戶輸入多個字元串並存儲在字元串數組中

int n = Integer.valueOf(input.nextLine().replaceAll("[^\d]", ""));

改成這樣即可;因為你的輸入函數,你之前調用的它int類型方法;

改成這樣,就類似你一直用的都是字元串,區別你第一次的字元串被Integer對象轉成數字了;

『玖』 Java中,怎樣把字元串和整數存到同一個數組中


importjava.util.Arrays;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;

publicclassTest{

publicstaticvoidmain(String[]args){
intlength=5;
HashMap[]map=newHashMap[length];
//向數組添加兩個數,一個是String,一個是int
booleansetValue=setValue(map,0,1);
if(setValue){
System.out.println("添加成功!");
}
booleansetValue1=setValue(map,1,"a");
if(setValue1){
System.out.println("添加成功!");
}
//取出數組中第index位的值
intindex=1;
Iteratorit=map[index].keySet().iterator();
Objectkey;
while(it.hasNext()){
key=it.next();
if(key.equals("String")){
//StrValue為得到的String值
StringStrValue=(String)map[index].get(key);
System.out.println(key+":"+StrValue);
}else{
//intValue為得到的int值
intintValue=(int)map[index].get(key);
System.out.println(key+":"+intValue);
}
}
}

/**
*@paramindex向第幾位添加
*@paramvalue添加的內容
**/
publicstaticbooleansetValue(HashMap[]map,intindex,Stringvalue){
if(index>map.length)
returnfalse;
HashMap<String,String>hashMap=newHashMap<String,String>();
hashMap.put("String",value);
map[index]=hashMap;
returntrue;
}

/**
*@paramindex向第幾位添加
*@paramvalue添加的內容
**/
publicstaticbooleansetValue(HashMap[]map,intindex,intvalue){
if(index>map.length)
returnfalse;
HashMap<String,Integer>hashMap=newHashMap<String,Integer>();
hashMap.put("Integer",value);
map[index]=hashMap;
returntrue;
}
}

『拾』 java中如何拆分字元串,存放到一個數組中

方法:

先拆分,然後把拆分的字元串存到數據組中即可,代碼參考

publicclassSTest
{
publicstaticvoidmain(String[]args)
{
Stringt="abc,edf,xyz";
String[]chrstr=t.split(",");
for(inti=0;i<chrstr.length;i++)
{
System.out.println(chrstr[i]);
}
}
}
閱讀全文

與java字元串存入數組相關的資料

熱點內容
不會數學英語如何編程 瀏覽:88
如何能知道網站伺服器地址 瀏覽:648
程序員月薪5萬難嗎 瀏覽:138
如何評價程序員 瀏覽:802
雲虛機和伺服器的區別 瀏覽:403
廣西柳州壓縮機廠 瀏覽:639
arm開發編譯器 瀏覽:833
51單片機的核心 瀏覽:746
看電視直播是哪個app 瀏覽:958
將c源程序編譯成目標文件 瀏覽:787
再要你命3000pdf 瀏覽:558
ai軟體解壓軟體怎麼解壓 瀏覽:520
文件夾怎樣設置序列號 瀏覽:963
javascriptgzip壓縮 瀏覽:248
易語言怎麼取出文件夾 瀏覽:819
蘋果xs手機加密app哪裡設置 瀏覽:605
超聲霧化器與壓縮霧化器 瀏覽:643
模擬實現進程調度演算法 瀏覽:388
現在的壓縮包都是加密 瀏覽:331
施工員找工作去哪個app 瀏覽:632