代碼如下:
創(chuàng)新互聯(lián)主營邛崍網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,重慶APP開發(fā),邛崍h5成都微信小程序搭建,邛崍網(wǎng)站營銷推廣歡迎邛崍等地區(qū)企業(yè)咨詢
import?java.util.Arrays;
class?Circle?{
private?int?radius;
public?Circle(int?radius)?{
this.radius?=?radius;
}
public?int?getRadius()?{
return?radius;
}
public?void?setRadius(int?radius)?{
this.radius?=?radius;
}
@Override
public?String?toString()?{
return?"Circle?[radius="?+?radius?+?"]";
}
}
public?class?App?{
public?static?void?main(String[]?args)?throws?CloneNotSupportedException?{
//?創(chuàng)建一個包含5個元素的數(shù)組
Circle[]?circles?=?{?new?Circle(2),?new?Circle(10),?new?Circle(8),?new?Circle(4),?new?Circle(12)?};?
System.out.println(Arrays.toString(circles));
//?排序
Arrays.sort(circles,?(x,?y)?-?Integer.compare(x.getRadius(),?y.getRadius()));
System.out.println(Arrays.toString(circles));
//?查找半徑為?9?的圓
int?index?=?Arrays.binarySearch(circles,?9,?(x,?y)?-?((Circle)x).getRadius()?-?(int)y);
System.out.println(index?=0???circles[index]?:?"沒有找到半徑為?9?的圓。");
//?查找半徑為?10?的圓
index?=?Arrays.binarySearch(circles,?10,?(x,?y)?-?((Circle)x).getRadius()?-?(int)y);
System.out.println(index?=0???circles[index]?:?"沒有找到半徑為?10?的圓。");
//?拷貝數(shù)組
Circle[]?circles2?=?Arrays.copyOf(circles,?circles.length);
System.out.println(Arrays.toString(circles2));
}
}
花了我將近一個小時的時間擺弄,你還不舍得給分
第一個類
/**************************************************************************
* 該類為啟動類,運行該類,將跳出輸入數(shù)組對話框,輸入的數(shù)字之間用逗號隔開,若輸入
* 的不是數(shù)字有可能出現(xiàn)異常,請自己處理。輸入的數(shù)字最大個數(shù)為100,也可以修改處理
* 更大個數(shù)的數(shù)組。
**************************************************************************/
package terry.test;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.EventListener;
import java.util.StringTokenizer;
public class InputData extends JFrame implements ActionListener
{
Container con=this.getContentPane();
JButton button1=new JButton("確定");
JButton button2=new JButton("取消");
JLabel label=new JLabel("請輸入數(shù)組:");
JTextField text=new JTextField("數(shù)字之間用逗號隔開");
Panel panel1=new Panel();
Panel panel2=new Panel();
@SuppressWarnings("deprecation")
public InputData()
{
super("輸入有序數(shù)據(jù)對話框");
con.setLayout(new GridLayout(2,1));
panel1.add(label);
panel1.add(text);
con.add(panel1);
button1.addActionListener(this);
panel2.add(button1);
button2.addActionListener(this);
panel2.add(button2);
con.add(panel2);
this.setSize(300,200);
this.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
String dataString=text.getText();//截取寫入的數(shù)組,現(xiàn)在還是一個字符串
ordArray arr=new ordArray(100);//生成排序類的實例;
//以下為處理整個字符串,轉(zhuǎn)化為整型數(shù)組。
if(dataString!=null)
{
StringTokenizer s=new StringTokenizer(dataString,",");
while(s.hasMoreTokens())
{
int temp=0;
try
{
temp=(new Integer(s.nextToken())).intValue();
}catch(NumberFormatException ex)
{
JOptionPane.showMessageDialog(null, "在數(shù)組中,請輸入整數(shù)值!");
}
arr.insert(temp);
}
}
this.dispose();
new InputSearchApp(arr);
}
}
public static void main(String args[])
{
new InputData();
}
}
第二個類
/**************************************************
* InputData實例向該類的實例傳遞了orderArray參數(shù)變量*
* *
*/
package terry.test;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class InputSearchApp extends JFrame implements ActionListener{
Container con=this.getContentPane();
JButton button1=new JButton("確定");
JButton button2=new JButton("取消");
JLabel label=new JLabel("請輸入要查找的數(shù)值:");
JTextField text=new JTextField(10);
ordArray arr=null;
Panel panel1=new Panel();
Panel panel2=new Panel();
public InputSearchApp(ordArray testArray)
{
super("輸入有序數(shù)據(jù)對話框");
arr=testArray;
con.setLayout(new GridLayout(2,1));
panel1.add(label);
panel1.add(text);
con.add(panel1);
button1.addActionListener(this);
panel2.add(button1);
button2.addActionListener(this);
panel2.add(button2);
con.add(panel2);
this.setSize(200,200);
this.show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==button1)
{
String dataString=text.getText();
int searchKey= (new Integer(dataString)).intValue();
boolean success=arr.find(searchKey);
if(success)
{
this.dispose();
JOptionPane.showMessageDialog(null, ("查找到數(shù)據(jù)"+searchKey));
}
else
{
JOptionPane.showMessageDialog(null, ("沒有查找到數(shù)據(jù)"+searchKey));
}
}
}
}
第三個類2分查找類
package terry.test;
public class ordArray {
private int[]a;
private int nElems;
public ordArray(int max)
{
a=new int[max];
nElems=0;
}
public int size()
{
return nElems;
}
public boolean find(int searchKey)
{
return recFind(searchKey,0,nElems-1);
}
private boolean recFind(int searchKey,int lowerBound,int upperBound)
{
int curIn,theindex;
//boolean flag=false;
curIn=(lowerBound+upperBound)/2;
if(a[curIn]==searchKey)
theindex=curIn;
else if(lowerBoundupperBound)
theindex=nElems;
else
{
if(a[curIn]searchKey)
return recFind(searchKey,lowerBound,curIn-1);
else
return recFind(searchKey,curIn+1,upperBound);
}
if(theindex!=this.size())
return true;
else
return false;
}
public void insert(int value)
{
int j;
for(j=0;jnElems;j++)
if(a[j]value)
break;
for(int k=nElems;kj;k--)
a[k]=a[k-1];
a[j]=value;
nElems++;
}
}
做什么事都要學會GOOGLE。
搜索JAVA記事本。
你新建一個類叫TEST。
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JFrame;
public class Test extends JFrame implements ActionListener {
/**
* Method main
*
*
* @param args
*
*/
MenuBar menuBar = new MenuBar();
Menu file = new Menu("File"), edit = new Menu("Edit"), help = new Menu(
"Help");
MenuItem[] menuItem = { new MenuItem("New"), new MenuItem("Open"),
new MenuItem("Save"), new MenuItem("Exit"),
new MenuItem("Select All"), new MenuItem("Copy"),
new MenuItem("Cut"), new MenuItem("Paste"), new MenuItem("Help") };
TextArea textArea = new TextArea();
String fileName = "NoName";
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolKit.getSystemClipboard();
// opne and close message dialogs
private FileDialog openFileDialog = new FileDialog(this, "Open File",
FileDialog.LOAD);
private FileDialog saveFileDialog = new FileDialog(this, "Save File",
FileDialog.SAVE);
public static void main(String[] args) {
// TODO: Add your code here
Test MyEdit = new Test();
MyEdit.show();
}
/**
* Method MiniEdit
*
*
*/
public Test() {
// TODO: Add your code here
setTitle("MiniEdit");
setFont(new Font("Times New Roman", Font.PLAIN, 15));
setBackground(Color.white);
setSize(500, 500);
setMenuBar(menuBar);
menuBar.add(file);
menuBar.add(edit);
menuBar.add(help);
for (int i = 0; i 4; i++) {
file.add(menuItem[i]);
edit.add(menuItem[i + 4]);
}
help.add(menuItem[8]);
add(textArea);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(0);
}
});
// add actionListener
for (int i = 0; i menuItem.length; i++) {
menuItem[i].addActionListener(this);
}
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
// TODO: Add your code here
Object eventSource = e.getSource();
if (eventSource == menuItem[0])// newItem
{
textArea.setText("");
}
else if (eventSource == menuItem[1])// OpenItem
{
openFileDialog.show();
fileName = openFileDialog.getDirectory() + openFileDialog.getFile();
if (fileName != null) {
openFile(fileName);
}
}
else if (eventSource == menuItem[2])// SaveItem
{
saveFileDialog.show();
fileName = saveFileDialog.getDirectory() + saveFileDialog.getFile();
if (fileName != null) {
writeFile(fileName);
}
}
else if (eventSource == menuItem[3])// exitItem
{
System.exit(0);
}
else if (eventSource == menuItem[4])// Select All
{
textArea.selectAll();
} else if (eventSource == menuItem[5])// copy
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
else if (eventSource == menuItem[6])// cut
{
String text = textArea.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
textArea.replaceText("", textArea.getSelectionStart(), textArea
.getSelectionEnd());
}
else if (eventSource == menuItem[7])// Paste
{
Transferable contents = clipboard.getContents(this);
if (contents == null)
return;
String text;
text = "";
try {
text = (String) contents
.getTransferData(DataFlavor.stringFlavor);
} catch (Exception ex) {
}
textArea.replaceText(text, textArea.getSelectionStart(), textArea
.getSelectionEnd());
} else if (eventSource == menuItem[8]) {
// JOptionPane.showMessageDialog(null,"This is a MiniEdit.");
}
}
// Read file
public void openFile(String fileName) {
try {
File file = new File(fileName);
FileReader readIn = new FileReader(file);
int size = (int) file.length();
int charsRead = 0;
char[] content = new char[size];
while (readIn.ready())
charsRead += readIn.read(content, charsRead, size - charsRead);
readIn.close();
textArea.setText(new String(content, 0, charsRead));
} catch (Exception e) {
System.out.println("Error opening file!");
}
}
// write file
public void writeFile(String fileName) {
try {
File file = new File(fileName);
FileWriter write = new FileWriter(file);
write.write(textArea.getText());
write.close();
} catch (Exception e) {
System.out.println("Error closing file!");
}
}
}
Jython(原JPython),是一個用Java語言寫的Python解釋器。
在沒有第三方模塊的情況下,通常選擇利用Jython來調(diào)用Python代碼,
它是一個開源的JAR包,你可以到官網(wǎng)下載
一個HelloPython程序
import?org.python.util.PythonInterpreter;
public?class?HelloPython?{
public?static?void?main(String[]?args)?{
PythonInterpreter?interpreter?=?new?PythonInterpreter();
interpreter.exec("print('hello')");
}
}
什么是PythonInterpreter?它的中文意思即是“Python解釋器”。我們知道Python程序都是通過解釋器來執(zhí)行的,我們在Java中創(chuàng)建一個“解釋器”對象,模擬Python解釋器的行為,通過exec("Python語句")直接在JVM中執(zhí)行Python代碼,上面代碼的輸出結(jié)果為:hello
在Jvm中執(zhí)行Python腳本
interpreter.execfile("D:/labs/mytest/hello.py");
如上,將exec改為execfile就可以了。需要注意的是,這個.py文件不能含有第三方模塊,因為這個“Python腳本”最終還是在JVM環(huán)境下執(zhí)行的,如果有第三方模塊將會報錯:java?ImportError:?No?module?named?xxx
僅在Java中調(diào)用Python編寫的函數(shù)
先完成一個hello.py代碼:
def?hello():
return?'Hello'
在Java代碼中調(diào)用這個函數(shù):
import?org.python.core.PyFunction;
import?org.python.core.PyObject;
import?org.python.util.PythonInterpreter;
public?class?HelloPython?{
public?static?void?main(String[]?args)?{
PythonInterpreter?interpreter?=?new?PythonInterpreter();
interpreter.execfile("D:/labs/hello.py");
PyFunction?pyFunction?=?interpreter.get("hello",?PyFunction.class);?//?第一個參數(shù)為期望獲得的函數(shù)(變量)的名字,第二個參數(shù)為期望返回的對象類型
PyObject?pyObject?=?pyFunction.__call__();?//?調(diào)用函數(shù)
System.out.println(pyObject);
}
}
上面的代碼執(zhí)行結(jié)果為:Hello
即便只是調(diào)用一個函數(shù),也必須先加載這個.py文件,之后再通過Jython包中所定義的類獲取、調(diào)用這個函數(shù)。
如果函數(shù)需要參數(shù),在Java中必須先將參數(shù)轉(zhuǎn)化為對應的“Python類型”,例如:
__call__(new?PyInteger(a),?new?PyInteger(b))
a,b的類型為Java中的int型,還有諸如:PyString(String?string)、PyList(IteratorPyObject?iter)?等。
詳細可以參考官方的api文檔。
包含第三方模塊的情況:一個手寫識別程序
這是我和舍友合作寫的一個小程序,完整代碼在這里:
,界面上引用了core?java上的一段代碼。Python代碼是舍友寫的,因為在Python程序中使用了第三方的NumPy模塊,導致無法通過Jython執(zhí)行。下面這個方法純粹是個人思路,沒有深入查資料。?核心代碼如下:
import?java.io.*;
class?PyCaller?{
private?static?final?String?DATA_SWAP?=?"temp.txt";
private?static?final?String?PY_URL?=?System.getProperty("user.dir")?+?"\\test.py";
public?static?void?writeImagePath(String?path)?{
PrintWriter?pw?=?null;
try?{
pw?=?new?PrintWriter(new?FileWriter(new?File(DATA_SWAP)));
}?catch?(IOException?e)?{
e.printStackTrace();
}
pw.print(path);
pw.close();
}
public?static?String?readAnswer()?{
BufferedReader?br;
String?answer?=?null;
try?{
br?=?new?BufferedReader(new?FileReader(new?File(DATA_SWAP)));
answer?=?br.readLine();
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}
return?answer;
}
public?static?void?execPy()?{
Process?proc?=?null;
try?{
proc?=?Runtime.getRuntime().exec("python?"?+?PY_URL);
proc.waitFor();
}?catch?(IOException?e)?{
e.printStackTrace();
}?catch?(InterruptedException?e)?{
e.printStackTrace();
}
}
//?測試碼
public?static?void?main(String[]?args)?throws?IOException,?InterruptedException?{
writeImagePath("D:\\labs\\mytest\\test.jpg");
execPy();
System.out.println(readAnswer());
}
}
實際上就是通過Java執(zhí)行一個命令行指令。
文章標題:手寫識別代碼java 手寫識別代碼
網(wǎng)頁網(wǎng)址:http://jinyejixie.com/article46/doohjeg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設、面包屑導航、商城網(wǎng)站、云服務器、品牌網(wǎng)站設計、標簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)