【问题标题】:Access separately to each element from a list string分别访问列表字符串中的每个元素
【发布时间】:2021-06-14 10:59:22
【问题描述】:

我正在尝试从一个字符串对象访问每个元素并将其与另一个字符串连接:

当前输出:来自对象 -> hrsize

4,77GB
1,91GB
2,86GB

将 KB 转换为 GB 后,我需要为所有输出命名。 变成这样:

TotalMemory: 4,77GB
MemoryAvailable: 1,91GB
MemoryFree: 2,86GB

有了这个输出,我就可以把它转换成一个 Hashmap。 (k , v)

我尝试遍历列表以为每个值分配一个名称,但没有成功。

我会留下完整的代码:

class Example {
    static void main(String[] args) {
        int A = 5000000 //TotalMemory -> Kb
        int B = 2000000 // MemoryAvailable -> Kb
        int C = 3000000 // MemoryFree -> Kb
        
        int[] array = [ A, B, C ] // Convert all variables into an Array

        for(int i in array) {

def size = i // Converting each element from the object to be converted into MB GB or TB

// Memory can be converted in KB MB GB AND TERABYTE depending on its size
String hrSize = ""

try {
    int k = size
    double m = size / 1024 // bytes
    double g = size / 1048576 // bytes
    double t = size / 1073741824 // bytes

    DecimalFormat dec = new DecimalFormat("0.00")

    if (k > 1) {
      //If the size is more than 1kb but less than 1024 bytes, the output will be KiloBytes
        hrSize = dec.format(k).concat("KB")
    }
    if (m > 1) {
      //If the size is more than 1kb but less than 1048576 bytes, the output will be Megabytes
        hrSize = dec.format(m).concat("MB")
    }
    if (g > 1) {
      //If the size is more than 1kb and more than 1048576 but less than 1073741824 bytes, the output will be Gigabytes
        hrSize = dec.format(g).concat("GB")
    }
    if (t > 1) {
      //If the size is more than 1073741824 bytes the output will be Terabytes.
        hrSize = dec.format(t).concat("TB")
    }
} catch (Exception e) {
    println("This program ran into a problem, root cause" + e)

}
 println(hrSize)
        }

    }
}

我真的希望有人可以帮助我,因为我已经为此苦苦挣扎了好几天。 可能这是非常基本的事情,但我尽力单独解决它;不开心!

【问题讨论】:

    标签: loops groovy iterator


    【解决方案1】:

    你为什么不简单地使用地图文字:

    import java.text.*
    
    Map map = [ TotalMemory:5000000,  //-> Kb
                MemoryAvailable:2000000,  // -> Kb
                MemoryFree:3000000 ] // -> Kb
    
    def toSize = { int size ->
      String hrSize = ""
    
        int k = size
        int m = size >> 10 // bytes
        int g = size >> 20 // bytes
        int t = size >> 30 // bytes
    
        DecimalFormat dec = new DecimalFormat("0.00")
    
        if (k > 1) {
          //If the size is more than 1kb but less than 1024 bytes, the output will be KiloBytes
            hrSize = dec.format(k).concat("KB")
        }
        if (m > 1) {
          //If the size is more than 1kb but less than 1048576 bytes, the output will be Megabytes
            hrSize = dec.format(m).concat("MB")
        }
        if (g > 1) {
          //If the size is more than 1kb and more than 1048576 but less than 1073741824 bytes, the output will be Gigabytes
            hrSize = dec.format(g).concat("GB")
        }
        if (t > 1) {
          //If the size is more than 1073741824 bytes the output will be Terabytes.
            hrSize = dec.format(t).concat("TB")
        }
    
      hrSize
    }
    
    map.each{ it.value = toSize( it.value ) }
    
    println map
    

    打印:

    [TotalMemory:4.00GB, MemoryAvailable:1953.00MB, MemoryFree:2.00GB]
    

    您还可以使用一些类似 apache-commons 的库来为您进行尺寸格式化,这样您就不必重新发明轮子了

    【讨论】:

    • 您还可以通过以if (t > 1) 开头并使用else if 作为后续条件来反转if 链,以避免重复检查。
    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2019-06-29
    • 2020-06-21
    • 1970-01-01
    • 2020-01-09
    相关资源
    最近更新 更多