代碼如下:
創(chuàng)新互聯(lián)主營(yíng)邛崍網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開(kāi)發(fā),邛崍h5成都微信小程序搭建,邛崍網(wǎng)站營(yíng)銷(xiāo)推廣歡迎邛崍等地區(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)建一個(gè)包含5個(gè)元素的數(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]?:?"沒(méi)有找到半徑為?9?的圓。");
//?查找半徑為?10?的圓
index?=?Arrays.binarySearch(circles,?10,?(x,?y)?-?((Circle)x).getRadius()?-?(int)y);
System.out.println(index?=0???circles[index]?:?"沒(méi)有找到半徑為?10?的圓。");
//?拷貝數(shù)組
Circle[]?circles2?=?Arrays.copyOf(circles,?circles.length);
System.out.println(Arrays.toString(circles2));
}
}
花了我將近一個(gè)小時(shí)的時(shí)間擺弄,你還不舍得給分
第一個(gè)類(lèi)
/**************************************************************************
* 該類(lèi)為啟動(dòng)類(lèi),運(yùn)行該類(lèi),將跳出輸入數(shù)組對(duì)話框,輸入的數(shù)字之間用逗號(hào)隔開(kāi),若輸入
* 的不是數(shù)字有可能出現(xiàn)異常,請(qǐng)自己處理。輸入的數(shù)字最大個(gè)數(shù)為100,也可以修改處理
* 更大個(gè)數(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("請(qǐng)輸入數(shù)組:");
JTextField text=new JTextField("數(shù)字之間用逗號(hào)隔開(kāi)");
Panel panel1=new Panel();
Panel panel2=new Panel();
@SuppressWarnings("deprecation")
public InputData()
{
super("輸入有序數(shù)據(jù)對(duì)話框");
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();//截取寫(xiě)入的數(shù)組,現(xiàn)在還是一個(gè)字符串
ordArray arr=new ordArray(100);//生成排序類(lèi)的實(shí)例;
//以下為處理整個(gè)字符串,轉(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ù)組中,請(qǐng)輸入整數(shù)值!");
}
arr.insert(temp);
}
}
this.dispose();
new InputSearchApp(arr);
}
}
public static void main(String args[])
{
new InputData();
}
}
第二個(gè)類(lèi)
/**************************************************
* InputData實(shí)例向該類(lèi)的實(shí)例傳遞了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("請(qǐng)輸入要查找的數(shù)值:");
JTextField text=new JTextField(10);
ordArray arr=null;
Panel panel1=new Panel();
Panel panel2=new Panel();
public InputSearchApp(ordArray testArray)
{
super("輸入有序數(shù)據(jù)對(duì)話框");
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, ("沒(méi)有查找到數(shù)據(jù)"+searchKey));
}
}
}
}
第三個(gè)類(lèi)2分查找類(lèi)
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++;
}
}
做什么事都要學(xué)會(huì)GOOGLE。
搜索JAVA記事本。
你新建一個(gè)類(lèi)叫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),是一個(gè)用Java語(yǔ)言寫(xiě)的Python解釋器。
在沒(méi)有第三方模塊的情況下,通常選擇利用Jython來(lái)調(diào)用Python代碼,
它是一個(gè)開(kāi)源的JAR包,你可以到官網(wǎng)下載
一個(gè)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程序都是通過(guò)解釋器來(lái)執(zhí)行的,我們?cè)贘ava中創(chuàng)建一個(gè)“解釋器”對(duì)象,模擬Python解釋器的行為,通過(guò)exec("Python語(yǔ)句")直接在JVM中執(zhí)行Python代碼,上面代碼的輸出結(jié)果為:hello
在Jvm中執(zhí)行Python腳本
interpreter.execfile("D:/labs/mytest/hello.py");
如上,將exec改為execfile就可以了。需要注意的是,這個(gè).py文件不能含有第三方模塊,因?yàn)檫@個(gè)“Python腳本”最終還是在JVM環(huán)境下執(zhí)行的,如果有第三方模塊將會(huì)報(bào)錯(cuò):java?ImportError:?No?module?named?xxx
僅在Java中調(diào)用Python編寫(xiě)的函數(shù)
先完成一個(gè)hello.py代碼:
def?hello():
return?'Hello'
在Java代碼中調(diào)用這個(gè)函數(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);?//?第一個(gè)參數(shù)為期望獲得的函數(shù)(變量)的名字,第二個(gè)參數(shù)為期望返回的對(duì)象類(lèi)型
PyObject?pyObject?=?pyFunction.__call__();?//?調(diào)用函數(shù)
System.out.println(pyObject);
}
}
上面的代碼執(zhí)行結(jié)果為:Hello
即便只是調(diào)用一個(gè)函數(shù),也必須先加載這個(gè).py文件,之后再通過(guò)Jython包中所定義的類(lèi)獲取、調(diào)用這個(gè)函數(shù)。
如果函數(shù)需要參數(shù),在Java中必須先將參數(shù)轉(zhuǎn)化為對(duì)應(yīng)的“Python類(lèi)型”,例如:
__call__(new?PyInteger(a),?new?PyInteger(b))
a,b的類(lèi)型為Java中的int型,還有諸如:PyString(String?string)、PyList(IteratorPyObject?iter)?等。
詳細(xì)可以參考官方的api文檔。
包含第三方模塊的情況:一個(gè)手寫(xiě)識(shí)別程序
這是我和舍友合作寫(xiě)的一個(gè)小程序,完整代碼在這里:
,界面上引用了core?java上的一段代碼。Python代碼是舍友寫(xiě)的,因?yàn)樵赑ython程序中使用了第三方的NumPy模塊,導(dǎo)致無(wú)法通過(guò)Jython執(zhí)行。下面這個(gè)方法純粹是個(gè)人思路,沒(méi)有深入查資料。?核心代碼如下:
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();
}
}
//?測(cè)試碼
public?static?void?main(String[]?args)?throws?IOException,?InterruptedException?{
writeImagePath("D:\\labs\\mytest\\test.jpg");
execPy();
System.out.println(readAnswer());
}
}
實(shí)際上就是通過(guò)Java執(zhí)行一個(gè)命令行指令。
文章標(biāo)題:手寫(xiě)識(shí)別代碼java 手寫(xiě)識(shí)別代碼
網(wǎng)頁(yè)網(wǎng)址:http://www.rwnh.cn/article46/doohjeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、面包屑導(dǎo)航、商城網(wǎng)站、云服務(wù)器、品牌網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)