返回一个整数数组中最大子数组的和(二)

  这次,在上次的情况下增加了一些新的要求。

  ·  要求数组从文件读取。
  ·  如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保证你的程序能正常输出。

  好,下面就是对代码的构造。

  首先,写一个写入文件的方法,并在这个方法中产生随机数(因为想看一下在这个程序是在多少数据量级下能够正确输出,一个一个按太麻烦了)

  

 1 public static void Writer() {
 2         // 获得路径
 3         String filepath = System.getProperty("user.dir"); // 获取当前类的完整路径
 4         filepath += "\\file.txt";
 5         File file = new File(filepath);
 6         if (!file.exists()) {
 7             try {
 8                 file.createNewFile();
 9             } catch (IOException e) {
10                 e.printStackTrace();
11             }
12         }
13         try {
14             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
15             Random random = new Random();
16             for (int i = 0; i < 1000000; i++) {// 随机产生100000个随机数
17                 int nums = 99999 - Math.round(random.nextFloat() * 1000000.0f);// 生成大小在1000000以内的正负整数
18                 bw.write(Integer.toString(nums));
19                 bw.newLine();
20             }
21             bw.write("1");
22             bw.close();
23 
24         } catch (IOException e) {
25             e.printStackTrace();
26         }
27     }

 

  然后就是再从文件中读出这些数据,因为在读取的过程中,当遇到最后一个正数后面的数负数的话,就不会将后面的负数读取到,所以在写入文本的过程中,在最后加了一个“1”。

  

  下面是读入文件的代码

 1 public static String[] readToString(String filePath) {
 2         File file = new File(filePath);
 3         Long filelength = file.length(); // 获取文件长度
 4         byte[] filecontent = new byte[filelength.intValue()];// 将文件长度强制转换为整形储存到byte数组
 5         try {
 6             FileInputStream in = new FileInputStream(file);
 7             in.read(filecontent);
 8             in.close();
 9         } catch (FileNotFoundException e) {
10             e.printStackTrace();
11         } catch (IOException e) {
12             e.printStackTrace();
13         }
14 
15         String[] fileContentArr = new String(filecontent).split("\r\n");
16         return fileContentArr;// 返回文件内容,默认编码
17     }

  

  这里使用byte数组来进行存储,并且用String数组进行分割。但是使用这个方法不能很好的解决数据溢出的问题。此问题待以后优化。

  

  然后就是主函数。

 1 public static void main(String args[]) {
 2         Writer();
 3         String[] Arr = readToString("file.txt");
 4         int max = Integer.MIN_VALUE;// 设置成最小整数
 5         int sum = 0;// 记录数组个元素相加的和
 6         int[] x = new int[Arr.length];
 7         for (int i = 0; i < Arr.length; i++) {// 将个元素依次相加并进行判断
 8             x[i] = Integer.parseInt(Arr[i]);
 9             sum += x[i];
10             if (sum > max) {// 如果求得总和大于之前的最大值的话,就将sum赋值给max
11                 max = sum;
12             }
13         }
14         System.out.println("最大子数组的和为:" + max);
15     }

 

  在从文件中读入的数据都是String型的,所以这里需要将读入的数据进行类型转化。

 x[i] = Integer.parseInt(Arr[i]);

  然后接可以输出正确的数据了,当然,数据是比较多的,所以,只能估算一下咯。

 

实验截图:

 

刚刚看到一个大牛的博客,看到了解决数据溢出的解决方案。

转:

  

使用split进行大数据分割时内存溢出解决方案

某个文本文件中存储了60W条记录,以\r\n作为分隔符,现在需要从文本中一次性取出所有值并存放到一个string[]数组中。

StreamReader sr = new StreamReader(strFilePath, System.Text.UnicodeEncoding.GetEncoding("utf-8"));

string strContent = sr.ReadToEnd();

string[] strArr = strContent.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

以上方式非常不建议使用,当数据量很大时很容易抛出内存溢出异常,这是由于string类型自身的安全性导致的,不建议使用string类型的对象临时保存大量的数据。

我们应该采用下面的方式来进行大数据量的处理。

 

复制代码
List<string> List = new List<string>();
using (StreamReader _StreamReaderKey = new StreamReader(strTermCacheFilePath + fileInfo.Name))
{
string strLine = "";
while (!string.IsNullOrEmpty((strLine = _StreamReaderKey.ReadLine())))
{
List.Add(strLine);
}
}
复制代码

 

posted @ 2019-03-14 18:54  啾啾丶丶  阅读(125)  评论(0编辑  收藏  举报