2017-06-21 4 views
-2

文字列に数字があります。私は切り捨てて、ループまたは何らかの方法で配列に保持したいと思っています。 この配列を再度ループして、最小から最大までランク付けします。 これらのアレイのような結果を得る方法。 ありがとうございます。文字列から部分文字列に数値をフェッチして配列を保持する方法はありますか?

public class Main 
{ 
    static String input = "1,3,7,11,5,16,13,12,22,14"; 
    public static void main(String[] args) 
    { 
     /* 
     int[] keep = new int[11]; //How I can know if I don't know total index in first time.`enter code here` 
     keep[0] = 1; 
     keep[1] = 3; 
     keep[2] = 7; 
     keep[3] = 11; 
     keep[4] = 5; 
     keep[5] = 16; 
     keep[6] = 13; 
     keep[7] = 12; 
     keep[8] = 22; 
     keep[9] = 14; 

     int[] rank = new int[keep.length]; 
     rank[0] = 1; 
     rank[1] = 3; 
     rank[2] = 5; 
     rank[3] = 7; 
     rank[4] = 11; 
     rank[5] = 12; 
     rank[6] = 13; 
     rank[7] = 14; 
     rank[8] = 16; 
     rank[9] = 22; 
     */ 

     for (int i=0;i<rank.length;i++) 
     { 
      System.out.println(rank[i]); 
     } 

    } 

} 
+0

あなたの説明は非常にあいまいです。私はこの文字列を整数の配列に変換したいということを是正していますか?もしそうなら、あなたはstringの 'split'メソッドをチェックアウトすることから始めることができます。 –

答えて

1

あなたがすることができますStringからinteger

String[] splitted=input.split(","); 

int[] keep = new int[splitted.length]; 

for(int i=0;i<splitted.length;i++){ 
    keep[i]= Integer.parseInt(splitted[i]); 
} 
1

を変換し、あなたの入力文字列にsplitメソッドを使用する必要があります。

  • は、結果のコロン
  • ストリームに文字列を分割配列
  • map tha tはとても基本的には、配列

に結果を回す

  • 配列
  • の各要素を解析する整数へ:あなたはこのような何かを行うことができ

    String input = "1,3,7,11,5,16,13,12,22,14"; 
    int[] foo = Stream.of(input.split(",")).mapToInt(Integer::parseInt).toArray(); 
    
    System.out.println(Arrays.toString(foo)); 
    
    1

    String input = "1,3,7,11,5,16,13,12,22,14"; 
    
    String[] keepStrings = input.split(","); 
    int[] keep = new int[keepStrings.length]; 
    int[] rank = new int[keep.length];// create a rank array 
    
    // arrays can be duplicated only by doing field by field copy. Otherwise it may lead to aliasing. 
    for (int i = 0; i < keepStrings.length; i++) { 
        keep[i] = Integer.parseInt(keepStrings[i]); 
        rank[i] = keep[i]; 
    } 
    
    Arrays.sort(rank); 
    
    System.out.println(keep); //[1, 3, 7, 11, 5, 16, 13, 12, 22, 14] 
    System.out.println(rank); //[1, 3, 5, 7, 11, 12, 13, 14, 16, 22] 
    
    0
    public static void main(String[] args) { 
        String input = "1,3,7,11,5,16,13,12,22,14"; 
    
        String myArray[] = input.split(","); 
        bubbleMethod(myArray); 
    } 
    
    public static int[] setValuesToInt(String theArray[]) { 
        int myArray[] = new int[theArray.length]; 
        for (int i = 0; i < theArray.length; i++) { 
         myArray[i] = Integer.parseInt(theArray[i]); 
        } 
    
        return myArray; 
    } 
    
    public static void bubbleMethod(String theArray[]) { 
        int arrayToSort[] = setValuesToInt(theArray); 
    
        int n = arrayToSort.length; 
        int temp = 0; 
    
        for (int i = 0; i < n; i++) { 
         for (int j = 1; j < (n - i); j++) { 
    
          if (arrayToSort[j - 1] > arrayToSort[j]) { 
           temp = arrayToSort[j - 1]; 
           arrayToSort[j - 1] = arrayToSort[j]; 
           arrayToSort[j] = temp; 
          } 
    
         } 
        } 
    
        for(int a : arrayToSort){ 
         System.out.println(a); 
        } 
    } 
    
    関連する問題