标题:不知道为什么出错了!
取消只看楼主
笔墨痕干
Rank: 1
等 级:新手上路
威 望:1
帖 子:56
专家分:0
注 册:2014-3-24
结帖率:84.21%
已结贴  问题点数:16 回复次数:0 
不知道为什么出错了!
package 字符串比较;
import java.util.ArrayList;//动态数组的包
public class Test9 {
    private int count1=0;
    private int count2=0;
    private int n=0;
    private int m=0;
    void Print(String[] s,int n)
    {
        for(int i=0;i<n;i++)
            System.out.print(s[i]+" ");
    }
    void Max(String[] s,int n)
    {
        for(int i=0;i<n;i++)
          {   
            for(int j=i+1;i<n;j++)
            {
                if(s[i].length()<s[j].length())
                    {
                     String s1;
                     s1=s[i];
                     s[i]=s[j];
                     s[j]=s1;
                    
                    }
            }
        }
        System.out.println();
        int i=0;
        while(s[0].length()==s[i].length())
        {
            System.out.println("最大相同的子字符串为:"+s[i++]);
        }
        
    }
    void JG(String[] s1,String[] s2)
    {
        ArrayList  s3=new ArrayList();//动态数组的初始化
        for(int i=0;i<s1.length;i++)
            for(int j=0;j<s1.length;j++)
            if((s1[i].equals(s2[j]))==true)
                s3.add(s1[i]);
            
        int n=s3.size();
        String[] s=new String[n];
        System.out.println();
        System.out.print("相同的子函数为:");
        for(int i=0;i<n;i++)//动态数组的输出。
            {
              s[i]=(String)s3.get(i);
             System.out.print(s[i]+" ");
            }
        Max(s,n);
    }
    void BJ(String a,String b)
    {
        int L1=a.length();
        int L2=b.length();
        for(int i=L1;i>0;i--)//求字符串子字符串的个数
        {
            count1+=i;
        }
        for(int j=L2;j>0;j--)
        {
            count2+=j;
        }
        String[] s1=new String[count1];
        String[] s2=new String[count2];
        for(int i=0;i<a.length();i++)//求出a的子字符串
            for(int j=i+1;j<=a.length();j++)
            {
                s1[n]=a.substring(i,j);
                n++;
            }
        System.out.print("a的子字符串为:");
        Print(s1,count1);
        System.out.println();
        for(int i=0;i<b.length();i++)//求出b的子字符串
            for(int j=i+1;j<=b.length();j++)
               
            {
                s2[m]=b.substring(i,j);
                m++;
            }
        System.out.print("b的子字符串为:");
        Print(s2,count2);
        JG(s1,s2);
    }
    public static void main(String[] args)
    {
        String a="as+a";
        String b="as+wwww";
        if(a.equals(b)==true)
            System.out.println("最大的相同字符串是:"+a);
        else
        {
          Test9 t=new Test9();
          t.BJ(a,b);
        }
    }

}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
    at 字符串比较.Test9.Max(Test9.java:19)
    at 字符串比较.Test9.JG(Test9.java:54)
    at 字符串比较.Test9.BJ(Test9.java:88)
    at 字符串比较.Test9.main(Test9.java:99)
a的子字符串为:a as as+ as+a s s+ s+a + +a a
b的子字符串为:a as as+ as+w as+ww as+www as+wwww s s+ s+w s+ww s+www s+wwww + +w +ww +www +wwww w ww www wwww w ww www w ww w
相同的子函数为:a as as+ s s+ a
这是为什么出错啊!!!
搜索更多相关主题的帖子: package private public import 字符串 
2014-09-26 14:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-436626-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.054216 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved