2013-12-21 19 views
-1

有2個數組。 代碼[]和文本[]如何整理只包含字母的數組?

code[0] = 0, text[0] = "Monkey333banana" // has numbers in text 
code[1] = 100, text[1] = "Dog" 
code[2] = 200, text[2] = "Cat117" // has numbers in text 
code[3] = 300, text[3] = "Pig" 

我想拿起文本的數組[] 僅由字母沒有號碼。

code[1] = 100, text[1] = "Dog" 
code[3] = 300, text[3] = "Pig" 

而我想保存另一個新的數組。

newCode[0] = 100, text[0] = "Dog" 
newCode[1] = 300, newText[1] = "Pig" 

示例代碼:

String[] code = new String[4]; 
int[] text = new int[4]; 

code[0] = 0; 
code[1] = 100; 
code[2] = 200; 
code[3] = 300; 

text[0] = "Monkey333banana"; 
text[1] = "Dog"; 
text[2] = "Cat117"; 
text[3] = "Pig"; 

// *** MAGIC **** // 
// *** MAGIC **** // 
// *** MAGIC **** // 

for (int i=0; i<(proper size); i++) 
System.out.println(newCode[i]+" : "+newText[i]); 

結果:

  • 100:狗;
  • 300:豬;

我該怎麼做這個魔法?請幫幫我。謝謝!

對不起......對行爲輕率,我寫了magic也許有人認爲這是不好的,對不起......

+1

使用循環來分隔它們。你應該使用'class'來分組文本和代碼。 –

+1

您介紹'code'數組,但我沒有看到您在任何地方使用它。你的意思是輸出應該是「100:狗,300:豬」? – kba

+0

@kba對不起!我的錯。我編輯。謝謝! – Juntae

回答

2

我希望這有助於提供:兩個數組是相同的

  • 大小...

    public static void main (String[] args) { 
    
        ArrayList<Integer> list = new ArrayList<Integer>(); // store code[] value 
        ArrayList<String> listS = new ArrayList<String>(); // store string w/o numbers 
    
        int code[] = {0, 100, 200, 300}; //example 
        String text[] = {"M12", "Dog", "117Cat", "Pig"}; //example 
    
        for (int i = 0; i < text.length; i++) { 
    
         if (! text[i].matches(".*\\d+.*")) { // VERY IMP REGEX TO TEST IF STRING CONTAINS ANY NUMBER 
          list.add(i); // or add(code[i]); 
          listS.add(text[i]); 
         } 
        } 
    
        int newCode[] = new int[list.size()]; 
        int idx = 0; 
        for (int x : list) { 
         newCode[idx++] = x; 
        } 
    
        String newText[] = listS.toArray(new String[list.size()]); 
    
        for (int i=0; i < newCode.length; i++) { 
         System.out.println(newCode[i] + " : " + newText[i]); 
        } 
    } 
    
  • 而不是寫magic投入一些精力/研究張貼問題

+0

謝謝...我認爲這比寫我的髒代碼更清楚...對不起...它的工作原理。謝謝你,祝你有個美妙的聖誕節。 – Juntae

+0

'newCode'的結果不是代碼的值,它是索引號...我編輯了我的文字,這是我的錯誤,請檢查它,謝謝。 – Juntae

+0

plz檢查我的意見循環 –

0

那魔:

for(int i=0;i<text.length;i++) 
    textvalid[i]=true; //saying that all the strings are valid. We will mark them as unvalid later 
k=0; //a counter 
for(int i=0;i<text.length;i++) { //every string in the array 
    for(int j=0, char[] text1 = text.toCharArray();j<text1.length;j++)  //every character in the string. text1 is the char[] equivallent of text[i] 
     if(text1[j]<'A' || (text1[j] >'Z' && text1[j]<'a') || text1[j] <='z') //if it is not a capital letter nor a lowercase one 
      textvalid[i] = false; //mark it as a npn-valid string 
    if(textvalid[i]==true){ //if it is valid, add it to the newtext array 
     newText[k++] = text[i]; 
     newCode[k] = code[i]; 
    } 
} 

希望它能幫助。 :)

0

這裏我發佈一條提示,進一步它是你的責任,自己動手

for(char i = 'a'; i <= 'z'; i++){ 
System.out.println(i+" "); 
} 
1
String[] newText = new String[4]; 
    int[] newCode = new int[4]; 
    int j=0; 
    for(int i=0;i<text.length;++i){ 
     if(text[i].matches("\\A[a-zA-Z]+\\z")){ 
      newCode[j]=code[i]; 
      newText[j++]=text[i]; 
     } 
    } 

    for (int i=0; i<j; i++) 
     System.out.println(newCode[i]+" : "+newText[i]); 
相關問題