, если вы хотите что-то более похожее на набор изменений ... можете использовать Counter
from collections import Counter
def diff(a, b):
""" more verbose than needs to be, for clarity """
ca, cb = Counter(a), Counter(b)
to_add = cb - ca
to_remove = ca - cb
changes = Counter(to_add)
changes.subtract(to_remove)
return changes
lista = ['one', 'three', 'four', 'four', 'one']
listb = ['one', 'two', 'three']
In [127]: diff(lista, listb)
Out[127]: Counter({'two': 1, 'one': -1, 'four': -2})
# in order to go from lista to list b, you need to add a "two", remove a "one", and remove two "four"s
In [128]: diff(listb, lista)
Out[128]: Counter({'four': 2, 'one': 1, 'two': -1})
# in order to go from listb to lista, you must add two "four"s, add a "one", and remove a "two"
Если вы выполните поиск в Google: «stdout JTextArea», у вас будет несколько ссылок для решения вашей проблемы.
В последней ссылке buddybob расширяет java.io.OutputStream
для печати стандартного вывода на его JTextArea. Я добавил его решение ниже.
/*
*
* @(#) TextAreaOutputStream.java
*
*/
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
/**
* An output stream that writes its output to a javax.swing.JTextArea
* control.
*
* @author Ranganath Kini
* @see javax.swing.JTextArea
*/
public class TextAreaOutputStream extends OutputStream {
private JTextArea textControl;
/**
* Creates a new instance of TextAreaOutputStream which writes
* to the specified instance of javax.swing.JTextArea control.
*
* @param control A reference to the javax.swing.JTextArea
* control to which the output must be redirected
* to.
*/
public TextAreaOutputStream( JTextArea control ) {
textControl = control;
}
/**
* Writes the specified byte as a character to the
* javax.swing.JTextArea.
*
* @param b The byte to be written as character to the
* JTextArea.
*/
public void write( int b ) throws IOException {
// append the data as characters to the JTextArea control
textControl.append( String.valueOf( ( char )b ) );
}
}
TextAreaOutputStream
расширяет классjava.io.OutputStream
и переопределяет его перегрузку методаwrite(int)
, этот класс использует ссылку на экземпляр управленияjavax.swing.JTextArea
, а затем добавляет к нему вывод всякий раз, когда вызывается метод write (int b).Чтобы использовать класс
blockquote>TextAreaOutputStream
, [yo] u должен использовать:Использование
// Create an instance of javax.swing.JTextArea control JTextArea txtConsole = new JTextArea(); // Now create a new TextAreaOutputStream to write to our JTextArea control and wrap a // PrintStream around it to support the println/printf methods. PrintStream out = new PrintStream( new TextAreaOutputStream( txtConsole ) ); // redirect standard output stream to the TextAreaOutputStream System.setOut( out ); // redirect standard error stream to the TextAreaOutputStream System.setErr( out ); // now test the mechanism System.out.println( "Hello World" );
JTextArea
и установить выход системы в ваш классAddNumber
, чтобы ваши системные вызовы отправлялись наJTextArea
. – Mr. Polywhirl 7 November 2013 в 13:38