java - 我想在android中连续进行数据类型转换

标签 java android

enter image description here

嗨。

我正在制作一个使用 stringbuilder 从蓝牙接收数据的应用程序

并使其切片以使用另一个 Activity 。

图像显示了我想要制作的内容。

Q1。我应该使用什么 c->d, d->e ?

第二季度。会有很多数据,我想知道如何简化这个序列

******************** 已编辑********************

我已经通过向 Arraylist 添加值来进行练习。

但是在字符串数组中,没有.get(),所以我无法访问元素的长度。

public static ArrayList<String> randomValue = new ArrayList<>();
    public static int iDistance=0, xIAngle=0, yIAngle=0, zIAngle=0;
    public static String distance, xAngle, yAngle, zAngle;

       randomValue.add("12345090080070");
        randomValue.add("15640080085071");
       randomValue.add("16542070084074");
        randomValue.add("12645080087078");
        randomValue.add("21345084081060");
        randomValue.add("14785078075065");
        randomValue.add("13155079077077");
        randomValue.add("14623080078078");
        randomValue.add("14918086080078");
        randomValue.add("15684085082080");


        for (int i=0; i<randomValue.size(); i++){
            String a = randomValue.get(i); 
            String distance = a.substring(0,5); 
            String xAngle = a.substring(5,8); 
            String yAngle = a.substring(8,11); 
            String zAngle = a.substring(11,14); 

            //String to int
            iDistance = Integer.parseInt(distance);
            xIAngle = Integer.parseInt(xAngle);
            yIAngle = Integer.parseInt(yAngle);
            zIAngle = Integer.parseInt(zAngle);

        }

edited

最佳答案

看起来您只是在寻找字符串数组的 get 的等效项。要访问数组中的元素,语法为array[I],因此如果您使用的是字符串数组,则此行:

String a = randomValue.get(i); 

本来应该是:

String a = randomValue[i];

可以使用 Streams 来缩短转换序列的代码:

// this is the sequence of transformation starting with the sting builder "a"
List<String> randomValueWithLength14 = 
    Arrays.stream(a.toString().split(";")).filter(x -> x.length() == 14)
        .collect(Collectors.toList());

// this is the for loop shown in your code
for (int i=0; i<randomValueWithLength14.size(); i++){
    String s = randomValueWithLength14.get(i); 
    String distance = a.substring(0,5); 
    String xAngle = s.substring(5,8); 
    String yAngle = s.substring(8,11); 
    String zAngle = s.substring(11,14); 

    //String to int
    iDistance = Integer.parseInt(distance);
    xIAngle = Integer.parseInt(xAngle);
    yIAngle = Integer.parseInt(yAngle);
    zIAngle = Integer.parseInt(zAngle);

}

关于java - 我想在android中连续进行数据类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57768271/

相关文章:

java - Android自定义 ListView

java - 有没有办法提前识别 CronTrigger 的特定触发实例?

android - 谷歌电视 : is there a way to know the current channel/program being watched with user permission?

android - 构建 AOSP 并添加具有运行时权限的系统应用

java - 使用现有的 HashMap 创建 HashMap

Java 8 - 比较 2 个对象并标记差异

java - 避免对 Spring 配置类进行类路径扫描

java - 在 DialogFragment 中使用 getIntent() 和 getPackageInfo

java - 删除数据库中的所有表后,如何在数据库中创建表?

Android:视频不从原始文件夹播放