简体   繁体   English

如何在Java中存储,排序和打印2D字符串和双精度“数组”?

[英]How do I store, sort and print a 2D “array” of strings and doubles in Java?

I need to store the following data in a format so that it can be sorted by the Double value from the lowest value to the highest. 我需要以一种格式存储以下数据,以便可以按从最低值到最高值的Double值对其进行排序。 I do not know java very well at all. 我一点都不了解Java。

Index,         Value
"string1",     1.1134
"string2",     2.3469
"string4",     5.2365
"string3",     2.5597
...

I understand I can't use arrays for this. 我知道我不能为此使用数组。 So there are things like ArrayLists, Comparators, Maps, HashMaps and I can't find any similar example to follow with them. 因此,存在诸如ArrayLists,Comparators,Maps,HashMaps之类的东西,我找不到任何类似的示例可以跟随它们。

This post ( Sort a two dimensional array based on one column ) seems close, but I don't understand how it works. 这篇文章( 基于一列对二维数组进行排序 )似乎很接近,但是我不知道它是如何工作的。

Notes: 笔记:

  1. I will not know the elements ahead of time, so I wanted to dimension the array at runtime. 我不会提前知道元素,所以我想在运行时确定数组的尺寸。 I hope the design can implement something like "new 2D-ArrayThingy[ExternalArray.size]" 我希望设计可以实现“ new 2D-ArrayThingy [ExternalArray.size]”之类的东西。
  2. I will obtain all the elements, row by row, in a object based loop. 我将在基于对象的循环中逐行获取所有元素。 They will always be string followed by double. 它们将始终是字符串,后跟双精度。 So understanding how to populate the rows, one at a time is useful. 因此,了解如何一次填充一行非常有用。 I don't care if the order is string first or not. 我不在乎命令是否首先是字符串。

Sub-Questions: 次要问题:

  1. How to I store this data per my needs? 如何根据我的需要存储这些数据?
  2. How do I sort the data by the doubles values? 如何按double值对数据排序?
  3. How do I output the data (to screen for example)? 如何输出数据(例如到屏幕)?

TIA! TIA!

You could store them in a TreeMap which will return keys according to their natural order. 您可以将它们存储在TreeMapTreeMap将根据其自然顺序返回键。 In this case, you would have the Double as the key and the String as the value. 在这种情况下,您将把Double作为键,将String作为值。

final Map<Double,String> map = new TreeMap<>();
map.put(1.2, "String2");
map.put(1.0, "String1");
map.put(-1.0, "String0");
for(Double d : map.keySet()) {
    System.out.println(d + " = " + map.get(d));
}

Which outputs: 哪个输出:

-1.0 = String0 -1.0 =字符串0

1.0 = String1 1.0 =字符串1

1.2 = String2 1.2 =字符串2

You could put them in a TreeMap , as follows: 您可以将它们放在TreeMap ,如下所示:

Map<Double, String> map = new TreeMap<Double, String>();

Then, you can put the objects in through the Map.put() method. 然后,可以通过Map.put()方法放入对象。 The size of the map isn't fixed, so you can edit it at any time you like. 地图的大小不是固定的,因此您可以随时对其进行编辑。 Assuming you had two arrays, one of Doubles and one of Strings, that would be paired together, you could: 假设您有两个数组,一个是Doubles,另一个是Strings,可以配对在一起,则可以:

for (int i = 0; i < doubles.length; ++i)
{
    map.put(doubles[i], string[i]);
}

Then, to print this in order, all you have to do is: 然后,要按顺序打印,您要做的就是:

System.out.println(map);

Happy Coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java-如何排序2D数组? - Java - How do i sort 2D array? Java如何完全对2D字符串数组进行排序 - Java How do I sort a 2D string array entirely 如何打印二维数组? - How do I print a 2d array? 对包含字符串和整数的 2D 列表进行排序,按整数 asc 排序我该怎么做? (JAVA) - Sorting a 2D List containing both Strings and Integers, sort by integers asc how do i do? (JAVA) 如何在不费力的情况下以这种形式打印这个二维字符串数组(java)? - How can I print this 2d array of strings in this form without too much effort (java)? 如何对双精度数组进行冒泡排序? - How do I bubble sort an array of doubles? 在Java中,如何获取2D数组的值并将它们存储在具有不同列和行大小的另一个2D数组中? - In Java, how do I take the values of a 2D array and store them in another 2D array with different column and row size? 如何从文件(Java)将双精度数添加到2d数组中? - How to add doubles into a 2d array from a file (Java)? 如何获取Java以将文本文件存储在2D数组中并在所述数组中定位特定点? - How do I get java to store a text file in a 2D array and locate a specific point in said array? 如何在Clojure中创建一个原始的二维(2d)双精度数组? - How do I create a primitive two-dimensional (2d) array of doubles in Clojure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM