如何将字符串数组列表转换为对象列表

发布于 2024-10-19 11:36:29 字数 1357 浏览 1 评论 0原文

我想将列表转换为列表,以便新列表中的每个对象都包含每个 String[] 的第一个元素。

你知道这在java中是否可以实现吗?

例如:

public List<String[]> readFile(){
  String[]array1={"A","1.3","2.4","2.3"};
  String[]array2={"B","1.43","3.4","2.8"};
  String[]array3={"C","5.23","2.45","2.9"};

  List<String[]>ReadFile= new ArrayList<String[]>();

  ReadFile.add(array1);
  ReadFile.add(array2);
  ReadFile.add(array3);

  return ReadFile;
}

现在我想要一个方法,它将采用上面的 List ReadFile 以某种方式将字符串数组分割成一个 ID,该 ID 将是第一个元素“A”、“B”、“C”,另一部分将是数字的字符串数组,我将通过另一种方法将数字从 String 转换为 Double 类型。我已经有了转换为 double 的方法,但我需要能够跟踪 ID 字段,因为 ID 字段将用于标识数字数组。

一位朋友建议我创建一个对象,其中每个对象的一部分作为字符串 ID,另一部分作为数组。这是我不知道该怎么做的部分。

有人可以帮忙吗? 下面是我认为应该具有的方法声明,因此返回类型将是 List,其中每个数组已转换为包含两部分的对象。

public List<Object> CreateObject(List<String[]>ReadFile){

}

谢谢, 杰特诺里。

大家好,感谢您抽出时间来提供帮助。
我可以看到使用哈希表的好处。我现在正在努力实施它。我知道我可能有点偏离主题,但只是为了解释我想做的事情: 在我的项目中,我有 CSV 文件,其中包含有关基因表达水平的数据。我使用 OpenCSV 读取文件的方法返回一个 List(String[]),其中每个 String[] 都是文件中的一行。每行的第一个元素是变量名称(recA、ybjE 等)。该行的其余部分将是与该变量相关的数字数据。我想计算每个数字数组之间的皮尔逊相关性。我已经实现的方法已经为我做到了这一点,但我现在遇到的问题是我必须从数组中删除字符串值,然后才能通过迭代数组将字符串值转换为双精度值。在我通过仍然保持 ID 链接到行来设法计算每个双精度数组之间的相关性之后,我希望能够在相关性高于我将设置的阈值的基因之间绘制无向节点图(例如相关性高于 0.80)。我不知道我是否咬得太多了,但我有 30 天的时间来做这件事,我相信在像你们这样的人的帮助下我会度过难关。
抱歉,继续说一会儿。 谢谢, 杰特诺里。

I want to convert a List to a List so that each object on my new list includes the first element of each String[].

Do you know if this is possible to do in java?

for example:

public List<String[]> readFile(){
  String[]array1={"A","1.3","2.4","2.3"};
  String[]array2={"B","1.43","3.4","2.8"};
  String[]array3={"C","5.23","2.45","2.9"};

  List<String[]>ReadFile= new ArrayList<String[]>();

  ReadFile.add(array1);
  ReadFile.add(array2);
  ReadFile.add(array3);

  return ReadFile;
}

Now I want a method which will take the List ReadFile from above to somehow split the arrays of strings into an ID which will be the first element "A", "B", "C" and another part which would be the string array of numbers which I will put through another method to convert numbers from String to type Double. I have already got the method to convert to double but I need to be able to keep track of the ID field because the ID field will be used to identify the array of numbers.

A friend suggested that I create an Object where each objects has one part as a String ID and the other part as an array. That is the part which I do not know how to do.

Can anybody help please?
below is the method declaration which I believe I should have so the return type will be List where each array has been converted to an Object with two parts.

public List<Object> CreateObject(List<String[]>ReadFile){

}

Thanks,
Jetnori.

Hi all, Thank you for taking your time to help.
I can see the benefit of using HashTables. I am as of now trying to implement it. I know i might be sidetracking a little but just to explain what I am trying to do:
In my project I have CSV file with data about gene expression levels. The method that I use from OpenCSV to read the file returns a List(String[]) where each String[] is one row in the file. The first element of each row is variable name (recA, ybjE etc). The rest of the row will be numbers data related to that variable. I want to calculate Pearson's correlation between each of the number arrays. The method which I have got implemented already does that for me but the problem that I have now is that I had to remove the string values from my arrays before I could convert to double by iterating over the array. After I have managed to calculate the correlation between each array of doubles by still keeping the ID linked to the row, I want to be able to draw an undirected node graph between the genes that have a correlation higher than a threshold which I will set (for example correlation higher than 0.80). I don't know if i am biting more than i can chew but I have 30 days to do it and I believe that with the help of people like you guys I will get through it.
Sorry for going on for a bit.
thanks,
Jetnori.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一指流沙 2024-10-26 11:36:29

我同意 Alb 提供的答案,但是这是您朋友的建议,首先您需要一个类来表示数据。我已经包含了一个解析数据的构造函数和一个接受已解析数据的构造函数,具体取决于您如何看待事物。

public class NumberList {
  private double[] numbers;
  private String key;

  public NumberList(Strig key, double[] numbers){
    this.ley = key;
    this.numbers = numbers;
  }

  public NumberList(String[] inputList) {
    key = inputList[0];
    numbers = new double[inputList.length-1];
    for(int i=1;i<inputList.length;i++){
      numers[i-1] = Double.parseDouble(inputList[i]);
    }
  }

  public String getKey() {
    return key;
  }

  public double[] getNumbers() { 
    return numbers;
  }
}

然后您需要您的函数来生成列表:

public List<NumberList> CreateObject(List<String[]> ReadFile){
  ArrayList<NumberList> returnList = new ArrayList<NumberList>(ReadFile.size());
  for (String[] input : ReadFile) {
    returnList.add(new NumberList(input));
  }
  return returnList;
}

请注意,这使用解析数据的构造函数,如果您使用其他构造函数,则“CreateObject”函数将需要包含解析逻辑。

最后需要注意的是,java 中的标准约定是唯一大写的是类名和最终静态字段(全部大写并用下划线分隔),因此通常方法签名为:

public List<NumberList> createObject(List<String[]> readFile){
    ...
}

I agree with the answer Alb provided, however this is what your friend has suggested, first you need a class to represent the data. I have included a constructor that parses the data and one that accepts already parsed data, depending on how you like to think of things.

public class NumberList {
  private double[] numbers;
  private String key;

  public NumberList(Strig key, double[] numbers){
    this.ley = key;
    this.numbers = numbers;
  }

  public NumberList(String[] inputList) {
    key = inputList[0];
    numbers = new double[inputList.length-1];
    for(int i=1;i<inputList.length;i++){
      numers[i-1] = Double.parseDouble(inputList[i]);
    }
  }

  public String getKey() {
    return key;
  }

  public double[] getNumbers() { 
    return numbers;
  }
}

Then you need your function to generate the list:

public List<NumberList> CreateObject(List<String[]> ReadFile){
  ArrayList<NumberList> returnList = new ArrayList<NumberList>(ReadFile.size());
  for (String[] input : ReadFile) {
    returnList.add(new NumberList(input));
  }
  return returnList;
}

Note this uses the constructor that parses the data, if you use the other constructor then the "CreateObject" function would need to include the parsing logic.

Finally on a side note the standard convention in java is that the only thing that is capitalized are class names and final static fields (which appear in all caps sepearted by underscores), so conventionally the method signature would be:

public List<NumberList> createObject(List<String[]> readFile){
    ...
}
稚气少女 2024-10-26 11:36:29

听起来你需要一个 Map列表的一部分,它允许您通过键(在您的案例 ID 中)对事物进行索引。

Map<String, String[]> map = new Hashmap<String, String[]>();
for( String[] array : ReadFile ){
   map.put( array[0], array );
}

然后要获取“A”的值数组,您可以这样做:

String[] values = map.get( "a" );

如果您希望值是双精度而不是字符串,您需要在放置之前更改数组(map.put调用)我建议使用列表或其他集合也可以代替数组。您可能还想从这些值中删除 ID 部分,但我的代码不会这样做。

Sounds like you need a Map instead of a List, it lets you index things by a key (in your case ID).

Map<String, String[]> map = new Hashmap<String, String[]>();
for( String[] array : ReadFile ){
   map.put( array[0], array );
}

then to get the array of values for 'A' you would do:

String[] values = map.get( "a" );

If you want the values to be doubles instead of strings you'll want to change the array before putting it (the map.put call) I'd advise using a list or other collections instead of an array also. You also will probably also want to remove the ID part from these values, which my code does not do.

当梦初醒 2024-10-26 11:36:29
public class Split_ListwithIDs {

Hashtable<String, String[]> table = new Hashtable<String, String[]>();
Splitter spl ;

public Split_ListwithIDs(Splitter split){
    spl = split;
}

private void addEntry(String key , String[] vals){
    table.put(key, vals);
}

public void parseList(List<String[]> list){
    for(String[] entry : list){
        String[] temp = new String[entry.length - 1];
        System.arraycopy(entry, 1, temp, 0,entry.length - 1);
        addEntry(entry[0], spl.GetStringArrayOfNumbers(temp));
    }
}

class SplittingHelper implements Splitter{

    @Override
    public String[] GetStringArrayOfNumbers(String[] arr) {
        String[] strArray = null ;

        // implementation here
        return arr;
    }

}

interface Splitter {
    String[] GetStringArrayOfNumbers(String[] arr);
}

您将不得不使用哈希表

而不是对象列表。(我假设您需要使用第一个字母表作为键来搜索列表中的给定条目 - 如果您想使用列表 )。

SplittingHelper 方法中,提供自定义逻辑来解析数字字符串并返回另一个数字字符串[]。

public class Split_ListwithIDs {

Hashtable<String, String[]> table = new Hashtable<String, String[]>();
Splitter spl ;

public Split_ListwithIDs(Splitter split){
    spl = split;
}

private void addEntry(String key , String[] vals){
    table.put(key, vals);
}

public void parseList(List<String[]> list){
    for(String[] entry : list){
        String[] temp = new String[entry.length - 1];
        System.arraycopy(entry, 1, temp, 0,entry.length - 1);
        addEntry(entry[0], spl.GetStringArrayOfNumbers(temp));
    }
}

class SplittingHelper implements Splitter{

    @Override
    public String[] GetStringArrayOfNumbers(String[] arr) {
        String[] strArray = null ;

        // implementation here
        return arr;
    }

}

interface Splitter {
    String[] GetStringArrayOfNumbers(String[] arr);
}

}

You will have to use a Hashtable instead of a list of objects.( I am assuming that you will need to search through the list for a given entry using the First alphabet as key - This will be very laborious if you want to use a List ).

In the method SplittingHelper , provide your custom logic to parse the string of numbers and return another string[] of numbers.

比忠 2024-10-26 11:36:29

我不明白你的目标,但对于“一个由两部分组成的对象”,你可能会考虑将它们存储在哈希表中: http://download.oracle.com/javase/6/docs/api/java/util/Hashtable.html

I don't understand your goal, but for 'an object with 2 parts' you might consider storing them in a Hashtable: http://download.oracle.com/javase/6/docs/api/java/util/Hashtable.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文