简体   繁体   English

如何从array / ArrayList获取特定元素?

[英]How to get specific element from array/ArrayList?

I am trying to get the last element from the end of each line of my ArrayList /array. 我试图从我的ArrayList / array的每一行的末尾获取最后一个元素。

For example i have these records int my list: (don't mind the language, Greek names) 例如,我在列表中有以下记录:(不要介意语言,希腊名称)

Nikos Pappas : 321/2012999, C,8.53  
Marios Athanasiou : 321/2012001, A,6.89  
Stavroula Markou : 321/2011001, D,7.00  
Giannis Kallogirou : 321/2009005, ST, 6.89  
Nikoletta Stefanou : 321/2010001, D, 7.25  
Stathis Petrou : 321/2011002, E, 8.10  
Kostas Tzovalis : 321/2007667, C, 5.00  
Michalis Kotsis : 321/2009723, D, 7.12  
Maria Katsanou : 321/2012002, C, 5.50  

And I want to take: 8.53, 6.89,....,5.50 (marks of students) 我想取:8.53、6.89,....,5.50(学生的分数)

This is what I've made so far (the file is the file which contains the above info) 这就是我到目前为止所做的(该文件是包含上述信息的文件)

  public static void main(String[] args)  {

    BufferedReader br = null;
    ArrayList <String> list = new ArrayList <> ();

    try
    {
        br = new BufferedReader(new FileReader("C:/Users/Stefi/Desktop/java_ask3.txt"));
        String str;


        //System.out.print("mpika");

        while((str = br.readLine()) != null)
        {
            list.add(str);
            System.out.println(str);
        }
    }
    catch(IOException e)
    {
        System.out.println("Κάτι πήγε στραβά! Προσπάθησε πάλι.");
    }
    finally
    {
        try
        {
            if (br != null)
            {
                br.close();
            }
        }
        catch(IOException e2)
        {
            System.out.println("Δεν μπορεσα να κλείσω το αρχείο.");
        }

    }

int x = list.size();
    String [] array = new String[x];
    array = list.toArray(array);

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

    String [] f = new String[list.size()];
    String vathmos;

     for(int i=0; i<array.length; i++)
    {
        vathmos = list.get(i).replaceAll("\\D+", "");
        f[i] = vathmos;
    }


    System.out.println("\n\n\n Float Print\n");
    for(int i=0; i<array.length; i++)
    {
        System.out.println(f[i]);
    }

}

this is an example of my output: 这是我的输出示例:

3212012999853     
3212012001689    
3212011001700    
3212009005689

It takes all the numbers it finds. 它采用找到的所有数字。 But I don't know how to take only the last float number. 但是我不知道如何只取最后一个浮点数。 I know it's totally wrong to work like this, and work like this because I'll never get the float i want. 我知道像这样工作是完全错误的,因为我永远也不会得到想要的浮动。

I also tried to convert my list into array and take the last element but didn't work out. 我还尝试将列表转换为数组并采用最后一个元素,但没有解决。

你可以试试这个

String s = line.replaceAll(".*(\\d+\\.\\d+)$", "$1");

Split the line by a comma then get the last element of the resulting array 用逗号分隔行,然后得到结果数组的最后一个元素

String[] splitArr = line.split(",");
String lastNumber = splitArr[splitArr.length - 1];

But best solution would be to have a Student class as suggested in comments 但是最好的解决方案是按照评论中的建议安排一个学生班

If you store your records as Strings, you can use the substring method: 如果将记录存储为字符串,则可以使用substring方法:

grade = Double.parseDouble(array[i].substring(array[i].length() - 3)); if you are sure the grade is always in x.xx form at the end of each String. 如果您确定成绩始终以x.xx的形式出现在每个字符串的末尾。

Also consider creating a class that would contain all the information about students in seperate fields, for example: 还可以考虑创建一个班级,其中包含有关各个字段中学生的所有信息,例如:

public class Student{
    public String name;
    public String surname;
    public int age;
    public double grade;
}

Of course this is just an example, you may hide implementation and provide methods you want as well. 当然,这只是一个示例,您可以隐藏实现并提供所需的方法。 Then you could create any kind of collection of Student objects, so getting the specified value of any field for any student would be easy. 然后,您可以创建任何类型的Student对象的集合,因此为任何学生获取任何字段的指定值将很容易。

I can suggest couple String functions to add to your code. 我可以建议将几个String函数添加到您的代码中。 This is not tested code though - 虽然这不是经过测试的代码-

Using String.split(); 使用String.split();

//This will hold all the numbers as String
List<String> masterList = new ArrayList<String>();
while((str = br.readLine()) != null)
{
       String [] lineValues = String.split(",");
       int countElements = lineValues.Length();
       masterList.Add(lineValues[countElements - 1]);
}

Using lastIndexOf(); 使用lastIndexOf();

//This will hold all the numbers as String
List<String> masterList = new ArrayList<String>();
while((str = br.readLine()) != null)
{
       int lastIndex = str.lastIndexOf(",");           
       masterList.Add(str.substring(lastIndex, str.length() - 1));
}

Once you have the masterList populated, you can loop through it and extract the float using 填充masterList后,可以遍历该列表并使用提取浮点数

 Float.valueOf(masterList.get(i));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM