如何在Java中打印自上而下读取的ArrayList?

如何在Java中打印自上而下读取的ArrayList?,java,list,arraylist,mean,Java,List,Arraylist,Mean,因此,我能够根据程序的要求向ArrayList中添加值,但现在我需要打印这些值以便垂直读取,就像列表中的每个值一样,但不能打印成长行。下面是我的代码 public class Homework23Average { public static void main(String[] args) { ArrayList exes = new ArrayList (); double x; double y = 0; Scann

因此,我能够根据程序的要求向ArrayList中添加值,但现在我需要打印这些值以便垂直读取,就像列表中的每个值一样,但不能打印成长行。下面是我的代码

public class Homework23Average {

    public static void main(String[] args) {
        ArrayList exes = new ArrayList ();
        double x;
        double y = 0;
        Scanner inputStream = null;

        try {
            inputStream = new Scanner (new File ("RawData.txt"));
        }
        catch (FileNotFoundException e) {
            System.out.println ("File not found, program aborted:");
            System.exit (1);
        }
        int count = 0;
        while (inputStream.hasNextDouble ()) {
            count ++;
            x = inputStream.nextDouble ();
            y += x;
            if (x > y/count) // x values greater than the mean (y/count)
                exes.add (x);
        }
        System.out.println ("This value(s) greater than the mean (" + y/count + ") are (is):" + exes);
        System.out.println ("This is the sum of all x values: " + y);
        System.out.println ("This is the mean of the x values: " + y/count);
。。。我从水平打印的ArrayList中获得结果: i、 e

我需要垂直打印结果: i、 e

这在ArrayList中是可能的吗?
我应该使用其他方法来完成这项工作吗


请注意,这是我在“Hello World”之后用Java编写的第二个程序。

它本机不受支持,但您可以使用一个简单的循环:

for (Object o : exes)
    System.out.println(o);
或者使用regex kung fu制作一个班轮:

System.out.println(exes.toString().replaceAll("^.|.$", "").replace(", ", "\n"));
它将
toString()
的输出修改为所需的格式

或者在java 8中:

exes.forEach(System.out::println);

本机不支持它,但您可以使用一个简单的循环:

for (Object o : exes)
    System.out.println(o);
或者使用regex kung fu制作一个班轮:

System.out.println(exes.toString().replaceAll("^.|.$", "").replace(", ", "\n"));
它将
toString()
的输出修改为所需的格式

或者在java 8中:

exes.forEach(System.out::println);

本机不支持它,但您可以使用一个简单的循环:

for (Object o : exes)
    System.out.println(o);
或者使用regex kung fu制作一个班轮:

System.out.println(exes.toString().replaceAll("^.|.$", "").replace(", ", "\n"));
它将
toString()
的输出修改为所需的格式

或者在java 8中:

exes.forEach(System.out::println);

本机不支持它,但您可以使用一个简单的循环:

for (Object o : exes)
    System.out.println(o);
或者使用regex kung fu制作一个班轮:

System.out.println(exes.toString().replaceAll("^.|.$", "").replace(", ", "\n"));
它将
toString()
的输出修改为所需的格式

或者在java 8中:

exes.forEach(System.out::println);

如果是本地的,我只需要做很多小的数组列表。不推荐使用它,因为它需要一点内存,但当它是一个本地应用程序来获取信息时,我会让它更容易阅读。如果它是IDE的本地版本,我会在IDE中使用该版本或集合选项。我建议听从经验丰富的兽医的建议。他们将知道最佳做法。 另外,“for循环”也很好,只需获取您的信息和“for循环”的正常设置

结果看起来很好,如果您打印到文件,它会垂直显示

节目:

Fuel Cost
3.21
Tank Capacity
15
Month(s) On Tire
42
Mileage Driven Today
90
This is the total For Gas:  

48.15
**************************************
**************************************
 This is Your Est. Operation Cost [Fuel Cost 3.21  ]
[Car Fuel Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0 miles  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0 miles ]

Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
BUILD SUCCESSFUL (total time: 15 seconds)
文件看起来就像ArrayList设置。。。。。。漂亮的

***************************
Before

Your Cost Of Operations
[Fule Cost 3.21  , Car Fule Cost 48.15  , Life Left In Tire (Rough Est.)  7.800000000000004  , Miles Driven 90  , Trips In City Base on Miles Driven:   3.1666666666666665  , City Fuel Capacity Based on Average City Driving:  285.0  , Trips on Hwy Base on Miles Driven:  4.0  , City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.29 at 11:59:05 EDT
***************************
***************************

AFTER

Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:20:54 EDT
***************************
***************************
Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004 Months  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
***************************

仍然有括号,但对我来说,它更容易阅读。

如果是本地的,我只需要做很多小数组列表或数组。不推荐使用它,因为它需要一点内存,但当它是一个本地应用程序来获取信息时,我会让它更容易阅读。如果它是IDE的本地版本,我会在IDE中使用该版本或集合选项。我建议听从经验丰富的兽医的建议。他们将知道最佳做法。 另外,“for循环”也很好,只需获取您的信息和“for循环”的正常设置

结果看起来很好,如果您打印到文件,它会垂直显示

节目:

Fuel Cost
3.21
Tank Capacity
15
Month(s) On Tire
42
Mileage Driven Today
90
This is the total For Gas:  

48.15
**************************************
**************************************
 This is Your Est. Operation Cost [Fuel Cost 3.21  ]
[Car Fuel Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0 miles  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0 miles ]

Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
BUILD SUCCESSFUL (total time: 15 seconds)
文件看起来就像ArrayList设置。。。。。。漂亮的

***************************
Before

Your Cost Of Operations
[Fule Cost 3.21  , Car Fule Cost 48.15  , Life Left In Tire (Rough Est.)  7.800000000000004  , Miles Driven 90  , Trips In City Base on Miles Driven:   3.1666666666666665  , City Fuel Capacity Based on Average City Driving:  285.0  , Trips on Hwy Base on Miles Driven:  4.0  , City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.29 at 11:59:05 EDT
***************************
***************************

AFTER

Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:20:54 EDT
***************************
***************************
Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004 Months  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
***************************

仍然有括号,但对我来说,它更容易阅读。

如果是本地的,我只需要做很多小数组列表或数组。不推荐使用它,因为它需要一点内存,但当它是一个本地应用程序来获取信息时,我会让它更容易阅读。如果它是IDE的本地版本,我会在IDE中使用该版本或集合选项。我建议听从经验丰富的兽医的建议。他们将知道最佳做法。 另外,“for循环”也很好,只需获取您的信息和“for循环”的正常设置

结果看起来很好,如果您打印到文件,它会垂直显示

节目:

Fuel Cost
3.21
Tank Capacity
15
Month(s) On Tire
42
Mileage Driven Today
90
This is the total For Gas:  

48.15
**************************************
**************************************
 This is Your Est. Operation Cost [Fuel Cost 3.21  ]
[Car Fuel Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0 miles  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0 miles ]

Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
BUILD SUCCESSFUL (total time: 15 seconds)
文件看起来就像ArrayList设置。。。。。。漂亮的

***************************
Before

Your Cost Of Operations
[Fule Cost 3.21  , Car Fule Cost 48.15  , Life Left In Tire (Rough Est.)  7.800000000000004  , Miles Driven 90  , Trips In City Base on Miles Driven:   3.1666666666666665  , City Fuel Capacity Based on Average City Driving:  285.0  , Trips on Hwy Base on Miles Driven:  4.0  , City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.29 at 11:59:05 EDT
***************************
***************************

AFTER

Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:20:54 EDT
***************************
***************************
Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004 Months  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
***************************

仍然有括号,但对我来说,它更容易阅读。

如果是本地的,我只需要做很多小数组列表或数组。不推荐使用它,因为它需要一点内存,但当它是一个本地应用程序来获取信息时,我会让它更容易阅读。如果它是IDE的本地版本,我会在IDE中使用该版本或集合选项。我建议听从经验丰富的兽医的建议。他们将知道最佳做法。 另外,“for循环”也很好,只需获取您的信息和“for循环”的正常设置

结果看起来很好,如果您打印到文件,它会垂直显示

节目:

Fuel Cost
3.21
Tank Capacity
15
Month(s) On Tire
42
Mileage Driven Today
90
This is the total For Gas:  

48.15
**************************************
**************************************
 This is Your Est. Operation Cost [Fuel Cost 3.21  ]
[Car Fuel Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0 miles  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0 miles ]

Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
BUILD SUCCESSFUL (total time: 15 seconds)
文件看起来就像ArrayList设置。。。。。。漂亮的

***************************
Before

Your Cost Of Operations
[Fule Cost 3.21  , Car Fule Cost 48.15  , Life Left In Tire (Rough Est.)  7.800000000000004  , Miles Driven 90  , Trips In City Base on Miles Driven:   3.1666666666666665  , City Fuel Capacity Based on Average City Driving:  285.0  , Trips on Hwy Base on Miles Driven:  4.0  , City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.29 at 11:59:05 EDT
***************************
***************************

AFTER

Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:20:54 EDT
***************************
***************************
Your Cost Of Operations
[Fule Cost 3.21  ]
[Car Fule Cost 48.15  ]
[Life Left In Tire (Rough Est.)  7.800000000000004 Months  ]
[Miles Driven 90  ]
[Trips In City Base on Miles Driven:   3.1666666666666665  ]
[City Fuel Capacity Based on Average City Driving:  285.0  ]
[Trips on Hwy Base on Miles Driven:  4.0  ]
[City Fuel Capacity Based on Average Highway Driving:  360.0  ]
Entry Completed On: 2018.Jun.30 at 12:29:42 EDT
***************************

仍然有括号,但对我来说更容易阅读。

是的,只要写一个for循环,就可以将列表中的所有值打印在一行中。是的,只要写一个for循环,就可以将列表中的所有值打印在一行中。是的,只要写一个for循环,就可以将列表中的所有值打印在一行中。是的,只需编写一个for循环,将列表中的所有值打印在一行中,因为ArrayList中的toString()将以任何方式遍历列表,所以第一个选项会更好。您可以避免regex.or Java8 kungful
exes.forEach(o->System.out.println(o))的开销
@zapl:
exes.forEach(System.out::println)@JeffreyBosboom-arr,谢谢你,我想这在这种情况下不起作用。确实需要仔细阅读整个lambda语法:)@Bohemian:see--创建一个流不是必需的。您的第一个选项会更好,因为ArrayList中的toString()将以任何方式遍历列表。您可以避免regex.or Java8 kungful
exes.forEach(o->System.out.println(o))的开销
@zapl:
exes.forEach(System.out::println)@JeffreyBosboom-arr,谢谢你,我想这在这种情况下不起作用。确实需要仔细阅读整个lambda语法:)@Bohemian:see--创建一个流不是必需的。您的第一个选项会更好,因为ArrayList中的toString()将以任何方式遍历列表。您可以避免regex.or Java8 kungful
exes.forEach(o->System.out.println(o))的开销
@zapl:
exes.forEach(System.out::println)@JeffreyBosboom arr,谢谢你,我想