如何从文本文件创建传入数据的ArrayList

时间:2014-02-20 12:09:27

标签: java arraylist

我写了3个文本文件,其中包含3列Latitude, Longitude and Device_ID。每个文件具有50-100个不同的不同经度/纬度坐标和2/4 device_id之间。 我编写了从这些txt文件中读取的代码并将其打印在屏幕上。我的下一步是将此信息放入ArrayList中。我的目标是为每个文本文件获取3个ArrayLists,因此总共有9个ArrayLists。 我不知道如何创建一个可以获取数据的ArrayList?

这是我到目前为止所做的事情;

public static void main (String args []) throws FileNotFoundException
{       
    File textA = new File("C:/Users/Daniel Dold/Desktop/Routes/RoutesA.txt");
    File textB = new File("C:/Users/Daniel Dold/Desktop/Routes/RoutesB.txt");
    File textC = new File("C:/Users/Daniel Dold/Desktop/Routes/RoutesC.txt");
    Scanner scannerA = new Scanner(textA);
    Scanner scannerB = new Scanner(textB);
    Scanner scannerC = new Scanner(textC);
    int lineNumber = 1;
    while(scannerA.hasNextLine())
    {
        String line = scannerA.nextLine();
        System.out.println(line);
        lineNumber ++;
    }
    System.out.println();
    while(scannerB.hasNextLine())
    {
        String line = scannerB.nextLine();
        System.out.println(line);
        lineNumber ++;
    }
    System.out.println();
    while(scannerC.hasNextLine())
    {
        String line = scannerC.nextLine();
        System.out.println(line);
        lineNumber ++;
    }
}

打印结果是;

51.51092066     -0.14513969     430
51.51073703     -0.14553129     431
51.51059347     -0.14583707
51.51047662     -0.1461026
51.51049331     -0.14613747
51.51048997     -0.14616698
51.51040651     -0.14641106
51.51035643     -0.14649421
51.51032972     -0.14652103
51.51027296     -0.14666057
51.51022789     -0.1467517
51.51021286     -0.14695287
51.51020285     -0.14709234
51.51021119     -0.14715671
51.51039983     -0.14728009
51.51031302     -0.14770388
51.51023081     -0.14809616
51.51037396     -0.14816656
51.51053838     -0.1482477
51.51074914     -0.14835298
51.51081507     -0.14799356
51.51090271     -0.14752015
51.51114643     -0.14763012
51.5114227      -0.14775619
51.51160215     -0.14783665
51.51171149     -0.147264
51.51179829     -0.14679461
51.51195938     -0.14597654
51.51213215     -0.14510482
51.51223063     -0.14473334
51.5123045      -0.14447115
51.51207831     -0.14425389
51.51192098     -0.14410034
51.51178786     -0.14396623
51.51166058     -0.14383413
51.51149532     -0.14366381
51.51131795     -0.14348007
51.51114768     -0.14330506
51.51108133     -0.1434841
51.5109987      -0.1436866
51.51094236     -0.14381736
51.51105087     -0.1439441
51.51122406     -0.14414862
51.51129876     -0.14423512
51.51131169     -0.14427468
51.51130418     -0.14431558
51.51123699     -0.14445908
51.51112348     -0.14470249
51.5109649      -0.14504045

51.49647926     -0.11145785     428
51.49666837     -0.11155306     435
51.49686708     -0.11165432     440
51.49705494     -0.11173612
51.498177       -0.11215657
51.49827719     -0.1121968
51.49853767     -0.11243283
51.49874472     -0.11250794
51.49888665     -0.11248111
51.4992348      -0.11213779
51.50008135     -0.11133045
51.50027253     -0.11114135
51.50053885     -0.11094555
51.50085609     -0.11072829
51.50109735     -0.11061162
51.50132443     -0.11054724
51.50160827     -0.11052578
51.50174435     -0.11048287
51.50185371     -0.11040777
51.50200899     -0.1102294
51.50218096     -0.10991424
51.50235043     -0.10951861
51.50261506     -0.10873407
51.50284881     -0.10804206
51.50305        -0.10740772
51.5030717      -0.10729775
51.50320527     -0.10689139
51.50337306     -0.10640725
51.50340813     -0.10628253
51.5036018      -0.10536924
51.50370698     -0.10499373
51.50376291     -0.10474026
51.50376583     -0.1047121
51.50376333     -0.10467119
51.50374663     -0.10458469
51.50356298     -0.10458737
51.50334635     -0.10459274
51.50260672     -0.1046209
51.50251572     -0.10461688
51.5022227      -0.10462761
51.50189796     -0.10459676
51.50175353     -0.10461554
51.50147553     -0.10466248
51.50095627     -0.10468527
51.50058977     -0.10469064
51.50039943     -0.10467589
51.49902858     -0.10473221
51.49892088     -0.10472685
51.49889751     -0.10462694
51.49886954     -0.10456122
51.49879106     -0.104466
51.49874013     -0.10444119
51.49870966     -0.10443449
51.49867877     -0.10443248
51.49864788     -0.10443717
51.49861782     -0.10445192
51.49858443     -0.1044546
51.49855813     -0.10444656
51.49849468     -0.10441303
51.49834857     -0.10431379
51.49777667     -0.10360837
51.49697099     -0.1026012
51.49614942     -0.10152295
51.49600498     -0.10134056
51.49571442     -0.10100528
51.49571192     -0.10099657
51.49568228     -0.10096103
51.49566558     -0.10092817
51.49565264     -0.10086916 
51.49564554     -0.10079071
51.49563218     -0.10049432
51.49561089     -0.10034613
51.49555286     -0.10014765
51.49548231     -0.10003634
51.49514625     -0.09992704
51.49508363     -0.09991228
51.49504981     -0.09991631
51.49502184     -0.09993039
51.49499345     -0.0999619
51.49478471     -0.10047689
51.49477303     -0.10056406
51.49477219     -0.10061905
51.49478388     -0.10070353
51.4949851      -0.10117158
51.49527733     -0.10182738
51.49543013     -0.1023075
51.4955967      -0.10293178
51.49573446     -0.10340653
51.49591857     -0.104006
51.49614024     -0.10471612
51.49636358     -0.10537728
51.49647588     -0.10570653
51.49655102     -0.10591037
51.49680818     -0.10646425
51.49703527     -0.10692827
51.49718389     -0.10722331
51.49724567     -0.10737888
51.49729159     -0.10750428
51.4973254      -0.10765247
51.49733        -0.1077155
51.49732749     -0.10775842
51.49733167     -0.10782949
51.49720894     -0.10850273
51.49714715     -0.10867305
51.49685827     -0.10986529
51.49652681     -0.11124663

51.50177649     -0.05012445     426
51.50210374     -0.05050666     429
51.50253617     -0.0509908      436
51.50265346     -0.05072191     443
51.50274404     -0.05055025
51.50301702     -0.05011841
51.503242       -0.04975095
51.50362768     -0.04908308
51.50409182     -0.04814565
51.50424291     -0.04773125
51.50437147     -0.0473316
51.50446955     -0.04699029
51.50449084     -0.04689507
51.50460103     -0.04640624
51.50465362     -0.04618093
51.50527135     -0.04514828
51.50535983     -0.04511878
51.50566035     -0.04506781
51.50588239     -0.04475668
51.50633816     -0.04415586
51.50670044     -0.04277721
51.50676471     -0.04252575
51.50684818     -0.04212744
51.50690328     -0.04173316
51.50692748     -0.04136502
51.50694835     -0.04037864
51.50692999     -0.03981739
51.50689117     -0.0390315
51.50687072     -0.03860503
51.50672631     -0.03755696
51.50658149     -0.03651693
51.50652598     -0.03614276
51.5064279      -0.03572434
51.50622631     -0.03506183
51.50616495     -0.03487475
51.50611278     -0.03475807
51.50605226     -0.03466084
51.50579474     -0.03435977
51.50577554     -0.03434367
51.50566744     -0.03427662
51.50522293     -0.03414184
51.50495664     -0.03413178
51.50471706     -0.0341586
51.50449835     -0.03419347
51.50445328     -0.03420688
51.5042354      -0.03416396
51.50346907     -0.03374956
51.50327456     -0.03367446
51.50285382     -0.03346659
51.5025416      -0.03336198
51.50246897     -0.03333516
51.50195556     -0.03335796
51.50168591     -0.03337539
51.50139205     -0.03335528
51.50094458     -0.03334991
51.50028338     -0.03336332
51.50020991     -0.03338881
51.50017068     -0.0334277
51.50013812     -0.0335028
51.50008886     -0.03416665
51.49998951     -0.03468163
51.49991688     -0.03483452
51.49985343     -0.03489755
51.49964805     -0.03499008
51.49957459     -0.03506921
51.49964638     -0.03540851
51.49998617     -0.03537096
51.50039024     -0.03537498
51.50086777     -0.03544472
51.50147887     -0.03558017
51.5024314      -0.03577597
51.50331964     -0.03588326
51.50394907     -0.03604553
51.50432973     -0.03622524
51.50455846     -0.03638886
51.50489153     -0.03672547
51.50502426     -0.0369253
51.50520874     -0.03726594
51.505359       -0.03759987
51.50554014     -0.03820471
51.50564031     -0.0388243
51.5056712      -0.03928296
51.50566953     -0.0402271
51.50564616     -0.04049934
51.50559357     -0.04134424
51.50553847     -0.04186727
51.50532978     -0.04298575
51.50514947     -0.04372202
51.50479303     -0.04486061
51.50411185     -0.04632242
51.50325285     -0.04770778
51.50260672     -0.04881553
51.50207577     -0.04964702
51.50185037     -0.04999302

对于很多人来说,这可能是一个简单的问题,但如果你可以帮助我,那就很好,欢呼

6 个答案:

答案 0 :(得分:2)

GOOD LORD STOP

如果你到了管理 9个ArrayList 对象的地步,就像从文件中读取一样简单,那么你的设计就出错了。你想要每个文件3 ArrayList个?不,不,让我们采取面向对象的方法。

示例

public class Position
{
    private String longitude;
    private String latitude;
    private String id;

    public Position(String longitude, String latitude, String id) {
        this.longitude = longitude;
        this.latitude = latitude;
        this.id = id;
    }
}

当您阅读自己的价值观时,请创建一个ArrayList类型Position,然后填充它。

String longitude, latitude, id;
List<Position> positions = new ArrayList<Position>();
// Grab those values from their files

positions.add(new Position(logitude, latitude, id));

现在你有了一个更易于管理,但仍然可以改进的3 ArrayList个对象;每个文件一个。

答案 1 :(得分:0)

您可以创建ArrayList来保存每个文件的每一行,或者为文件创建3个不同的数组列表。

这样的事情:

ArrayList<String> arrayList = new ArrayList<String>();

然后要在ArrayList中添加一些东西,你应该使用add方法。

while(scannerA.hasNextLine())
{
    String line = scannerA.nextLine();
    arrayList.add(line);
    System.out.println(line);
    lineNumber ++;
}

通过阅读评论,我知道你想使用3个不同的ArrayList。 代码是一样的,只需添加3个ArrayList。 在while循环中,你应该使用一次arrayList1,第二个循环arrayList2等。

我使用了String,因为你在代码中使用它,如果你需要不同的类型,你应该更改它。

答案 2 :(得分:0)

您的Id列表需要是一个列表列表,因为您可以拥有多个ID。

因此,对于您将拥有的每个文件

List<Integer> LongList = new ArrayList<>;
List<Integer> LatList = new ArrayList<>;
List<List<Integer>> IdList = new ArrayList<>;

分割选项卡上的每一行,即

String[] parts = line.split(\t);

然后将每个部分添加到右边的'ArrayList',即

LongList.add(Double.parseDouble(parts[0]));
LatList.add(Double.parseDouble(parts[1]));
List<Integer> ids = new ArrayList<>();
IdList.add(ids);
for(int i =2;i<parts.length;i++)
    ids.add(Integer.parseInt(parts[i]));

然后你可以一起迭代它们。如果您没有ID,则IdList中会有一个空列表。

答案 3 :(得分:0)

如何使用ArrayList

声明一个arraylist

您的ArrayList很可能会填充双打。因此,要声明一个可以填充双打的ArrayList,请输入:

ArrayList<Double> list = new ArrayList<Double>();

这基本上只是说我想要创建一个arraylist并且它将充满双打

将项目添加到arraylist

这很简单。

只做list.add(Double)

马可只是打败了我。我没有删除这个,所以无论如何我都要发布它。

答案 4 :(得分:0)

您想使用3个ArrayLists存储文件行中的每个数据吗? 目前尚不清楚为什么要这样做,因为从你的输出中我看到最后一列对于某些行是空的。我建议创建一个类来存储来自一行的数据,并将每一行解析为将添加到数组中的类的新实例。您可以将Double用于纬度和经度,并在将结果存储在ArrayList中之前解析String。

public final class FileLine {

private final String latitude;
private final String longitude;
private final String[] deviceID;

public FileLine(final String latitude, final String longitude, final String[] deviceID) {
    this.latitude = latitude;
    this.longitude = longitude;
    this.deviceID = deviceID;
}

public String getLatitude() {
    return latitude;
}

public String getLongitude() {
    return longitude;
}

public String[] getDeviceID() {
    return deviceID;
}

}

    final ArrayList<FileLine> file1Lines = new ArrayList<FileLine>();
    while(scannerA.hasNextLine())
    {
        final String[] line = scannerA.nextLine().split(" ");                        
        if (line.length > 2) {
            file1Lines.add(new FileLine(line[0], line[1], Arrays.copyOfRange(line, 2, line.length - 1)));
        } else if (line.length == 2) {
            file1Lines.add(new FileLine(line[0], line[1], null));
        } else if (line.length == 1) {
            file1Lines.add(new FileLine(line[0], null, null));
        }
    }

答案 5 :(得分:0)

使用以下行

定义字符串的数组列表
List<String> list = new ArrayList<String>();

现在,您的行有两个或三个以空格分隔的值。您可以将空格用作分隔符并使用

拆分它们
 while(scannerA.hasNextLine())
  {
    String line = scannerA.nextLine();
    String[] values = line.split("    ");        
  }

迭代String []以获取值,并将每个值添加到使用上面给定行创建的三个数组中。