convert.java

来自「包括了5个基本排序过程」· Java 代码 · 共 71 行

JAVA
71
字号
//实现数据串文本与一维数组的转换
public class Convert 
{
	private static String str;
	private static double[] array;
	public Convert()
	{
	}
	public static String arraytostring(double [] arraytem)//实现数组到字符串的转换
	{
		str="";//这句很重要 不然界面类里的text2会积累显示。因为str是一个静态的变量
		for(int i=0;i<arraytem.length;i++)
		{
			str=str+Double.toString(arraytem[i])+" ";
		}
		return str;
	}
	public static double[] stringtoarray(String strtem)//实现字符串到数组的转换
	{
		array=null;
		try
		{
			strtem=strtem.trim();
			if(strtem.length()<=0) throw new NumberFormatException();
			double [] temp=new double[50];
			int j=0;
			for(;strtem.length()>0;j++)
			{
				if(strtem.indexOf(" ")>0)
					{
						temp[j]=Double.parseDouble(strtem.substring(0, strtem.indexOf(" ")));
						strtem=(strtem.substring(strtem.indexOf(" "))).trim();
					}
				else {
						temp[j]=Double.parseDouble(strtem.substring(0));
						strtem="";
						break;
					}
			}
			//此时temp中有j+1个double数据
			array=new double[j+1];
			for(int i=0;i<=j;i++)
			{
				array[i]=temp[i];
			}
		}
		catch (NumberFormatException e)
		{
			/*System.out.println(
            "Your input number is not correct.");
			System.out.println(
            "Your input number must be");
			System.out.println(
             "an ordinary number either with");
			System.out.println(
              "or without a decimal point,");
			System.out.println("such as 42 or 9.99");
			System.out.println("Please try again.");*/
			//MessageBox Blank=new MessageBox("警告","请正确的输入需要排列的数字");
			//Blank.setVisible(true);
			
		}
		catch(NullPointerException e2)
		{
			//MessageBox Blank=new MessageBox("警告","请正确的输入需要排列的数字");
			//Blank.setVisible(true);
		}
		return array;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?