Java 数组列表有问题
在类WordPair()中,remove()方法应该接受一个数组,并拆分其中的字符串,然后将这些字符串添加到两个不同的数组列表中。wordListB工作正常,数组填充良好。另外,当我在这个方法内部打印系统时,它也会正确地打印出wordListC。当在JlistFromFile类的wordSelection方法中调用getWord方法以从worldListC检索特定索引处的字符串时,我遇到的问题是,此时数组打印为空。我主要设置了一个测试,而班级本身也在按照预期的方式工作。我不明白是什么让ArrayList变得清晰Java 数组列表有问题,java,arrays,Java,Arrays,在类WordPair()中,remove()方法应该接受一个数组,并拆分其中的字符串,然后将这些字符串添加到两个不同的数组列表中。wordListB工作正常,数组填充良好。另外,当我在这个方法内部打印系统时,它也会正确地打印出wordListC。当在JlistFromFile类的wordSelection方法中调用getWord方法以从worldListC检索特定索引处的字符串时,我遇到的问题是,此时数组打印为空。我主要设置了一个测试,而班级本身也在按照预期的方式工作。我不明白是什么让Array
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.Collections;
import java.util.Arrays;
import java.io.*;
/* Simple example of using the contents of a file to populate a JList
*
* @author Jill Courte
*/
public class JListFromFile extends JFrame {
private TextField wordA;
private TextField wordB;
private JButton openButton;
private JButton newButton;
private JButton addButton;
private JButton deleteButton;
private JButton saveButton;
private TextField output;
private JList listFromFile;
private JPanel listPanel;
private JPanel textPanel;
private JPanel inputPanel;
private JPanel buttonsPanel;
private SimpleWordList dataSource;
private WordPair wordPair;
public JListFromFile ()
{
// create the object to provide the data
dataSource = new SimpleWordList();
wordPair = new WordPair();
// use a border layout for the main window
getContentPane().setLayout(new BorderLayout(20, 20));
buttonsPanel = new JPanel();
openButton = new JButton("Open");
newButton = new JButton("New");
addButton = new JButton("Add");
deleteButton = new JButton("Delete");
saveButton = new JButton("Save");
addButton.setEnabled( false );
deleteButton.setEnabled( false );
saveButton.setEnabled( false );
buttonsPanel.add(openButton);
buttonsPanel.add(newButton);
buttonsPanel.add(addButton);
buttonsPanel.add(deleteButton);
buttonsPanel.add(saveButton);
//add the button listeners
openButton.addActionListener(new OpenButtonListener());
addButton.addActionListener(new AddButtonListener());
add(buttonsPanel, BorderLayout.NORTH);
// create the panel to hold the list
listPanel = new JPanel();
// create your JList
listFromFile = new JList();
listFromFile.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listFromFile.setSelectedIndex(0);
//add the list selection listener
listFromFile.addListSelectionListener(new WordSelection());
//add the translation text box
textPanel = new JPanel();
output = new TextField();
output.setEditable(false);
output.setPreferredSize(new Dimension(200, 25));
textPanel.add(output);
// add scroll bars to list
JScrollPane listScrollPane = new JScrollPane(listFromFile);
listScrollPane.setPreferredSize(new Dimension(150, 200));
//add user input textfield
wordA = new TextField ("Word to Add");
wordB = new TextField ("Translated word");
wordA.setPreferredSize(new Dimension(200, 25));
wordB.setPreferredSize(new Dimension(200, 25));
wordA.setEnabled(false);
wordB.setEnabled(false);
//create panel to hold input textfield
inputPanel = new JPanel();
inputPanel.add(wordA);
inputPanel.add(wordB);
//add the list (via the scroll pane) to the panel
listPanel.add(listScrollPane);
//add the panels to the frame
add(listPanel, BorderLayout.WEST);
add(textPanel, BorderLayout.CENTER);
add(inputPanel, BorderLayout.SOUTH);
pack();
}
private File findFile ()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
// Start in current directory
chooser.setCurrentDirectory(new File("."));
int status = chooser.showOpenDialog(null);
if (status != JFileChooser.APPROVE_OPTION)
{
JOptionPane.showMessageDialog(null, "No File Selected");
throw new NoFileChoosenException();
}
else
{
File file = chooser.getSelectedFile();
System.out.println(file.getName());
System.out.println(file.getPath());
return file;
}
}
private class OpenButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
listFromFile.setListData(new Object[0]);
File file = null;
try
{
file = findFile();
//tell the data source what file to use
dataSource.createList(file);
//JList needs an array of strings, retrieve it from the data source
String [] data = dataSource.getData();
//data = dataSource.insertion(data);
//data = wordPair.remove(data);
if (data != null)
listFromFile.setListData(data);
}
catch (Exception e)
{
}
addButton.setEnabled(true);
deleteButton.setEnabled(true);
saveButton.setEnabled(true);
wordA.setEnabled(false);
wordB.setEnabled(false);
}
}
private class AddButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
wordA.setEnabled(true);
wordB.setEnabled(true);
String inputStringA = wordA.getText();
String inputStringB = wordB.getText();
//dataSource.addToList(inputStringA, inputStringB);
String [] data2 = dataSource.getData();
//data2 = dataSource.insertion(data2);
// data2 = wordPair.remove(data2);
listFromFile.setListData(new Object[0]);
listFromFile.setListData(data2);
}
}
private class WordSelection implements ListSelectionListener
{
public void valueChanged (ListSelectionEvent event)
{
int index = listFromFile.getSelectedIndex();
if (index >= 0)
{
String s = wordPair.getWord(index);
output.setText(s);
}
}
}
public static void main(String[] args)
{
JListFromFile jListFromFile = new JListFromFile();
jListFromFile.setVisible(true);
}
}
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.ListIterator;
/*example of using collection class and input parsing for CIT 271
*@author Jill Courte
*/
public class SimpleWordList {
private ArrayList<String> wordListA = new ArrayList<String>();
private ArrayList<String> wordListB = new ArrayList<String>();
private ArrayList<String> wordListC = new ArrayList<String>();
private File currentFile = null;
private WordPair wordPair;
//words are in the file one word on a line
//words are read, checked for duplicates, and inserted into the file
public void createList (File file)
{
Scanner fileIn = null;
currentFile = file;
String line;
try
{
fileIn = new Scanner(new FileInputStream(currentFile.getPath()));
}
catch (IOException e)
{
System.out.println(e.getMessage());
System.exit(0);
}
while (fileIn.hasNextLine())
{
line = fileIn.nextLine();
if (! isDuplicate(line))
insertIntoList(line);
}
fileIn.close();
}
//Uses an iterator to check for the existance of the given
//word in the list
private boolean isDuplicate (String word)
{
Iterator<String> it = wordListA.iterator();
while (it.hasNext())
{
//not ignoring case to look in class at some issues with input
String s = it.next();
if (word.equals(s))
return true;
}
return false;
}
private void insertIntoList (String word)
{
int index = 0;
boolean positionFound = false;
int result;
while (! positionFound && index < wordListA.size())
{
String tmp = wordListA.get(index);
result = tmp.compareTo(word);
if (result > 0)
positionFound = true;
else
index++;
}
//index holds the position to insert the word
wordListA.add(index, word);
//System.out.println(word);
}
public String [] getData ()
{
String [] arr = new String[wordListA.size()];
wordPair = new WordPair();
String [] temp2 = wordPair.remove(wordListA.toArray(arr));
// use the collection class method to turn this into a
// primitive array
return temp2;
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.*;
public class WordPair
{
private ArrayList<String> wordListB;
private ArrayList<String> wordListC;
private String [] arrayA;
private String [] arrayB;
private String wordA;
private String wordB;
public String[] remove (String numList[])
{
wordListB = new ArrayList<String>();
wordListC = new ArrayList<String>();
ArrayList<String> ar = new ArrayList<String>();
for( int i=0; i < numList.length; i++)
{
String string = numList[i];
String[] parts = string.split("=");
wordA = parts[0];
wordListB.add(wordA);
wordB = parts[1];
wordListC.add(wordB);
}
System.out.println(wordListC);
arrayA = wordListB.toArray(new String[wordListB.size()]);
return arrayA;
}
public String getWord(int index)
{
System.out.println(wordListC);
return wordListC.get(index);
}
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入javax.swing.*;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
导入java.util.Collections;
导入java.util.array;
导入java.io.*;
/*使用文件内容填充JList的简单示例
*
*@作者吉尔·考特
*/
公共类JListFromFile扩展了JFrame{
私有文本字段wordA;
私有文本字段wordB;
私有JButton-openButton;
私人JButton-newButton;
私有JButton addButton;
私有JButton删除按钮;
私有JButton保存按钮;
专用文本字段输出;
私有JList-listFromFile;
私有JPanel列表面板;
私人JPanel文本面板;
专用JPanel输入面板;
私人JPanel按钮面板;
私有SimpleWordList数据源;
私有字对;
公共JListFromFile()
{
//创建对象以提供数据
dataSource=新的SimpleWordList();
wordPair=新的wordPair();
//对主窗口使用边框布局
getContentPane().setLayout(新的BorderLayout(20,20));
buttonsPanel=新的JPanel();
openButton=新的JButton(“打开”);
新按钮=新按钮(“新”);
addButton=新的JButton(“添加”);
deleteButton=新的JButton(“删除”);
saveButton=新的JButton(“Save”);
addButton.setEnabled(false);
deleteButton.setEnabled(错误);
saveButton.setEnabled(false);
按钮面板添加(打开按钮);
按钮面板添加(新按钮);
按钮面板添加(添加按钮);
按钮面板添加(删除按钮);
按钮面板添加(保存按钮);
//添加按钮侦听器
addActionListener(新的OpenButtonListener());
addButton.addActionListener(新的AddButtonListener());
添加(按钮面板,边框布局。北);
//创建用于保存列表的面板
listPanel=newjpanel();
//创建您的JList
listFromFile=new JList();
listFromFile.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listFromFile.setSelectedIndex(0);
//添加列表选择侦听器
listFromFile.addListSelectionListener(newWordSelection());
//添加翻译文本框
textPanel=newjpanel();
输出=新文本字段();
output.setEditable(false);
setPreferredSize(新维度(200,25));
textPanel.add(输出);
//将滚动条添加到列表中
JScrollPane listScrollPane=新的JScrollPane(listFromFile);
setPreferredSize(新维度(150200));
//添加用户输入文本字段
wordA=新文本字段(“要添加的单词”);
wordB=新文本字段(“翻译的单词”);
setPreferredSize(新维度(200,25));
设置首选尺寸(新尺寸(200,25));
wordA.setEnabled(false);
wordB.setEnabled(false);
//创建用于保存输入文本字段的面板
inputPanel=newJPanel();
inputPanel.add(wordA);
inputPanel.add(wordB);
//将列表(通过滚动窗格)添加到面板
添加(listScrollPane);
//将面板添加到框架中
添加(列表面板,BorderLayout.WEST);
添加(textPanel,BorderLayout.CENTER);
添加(输入面板,BorderLayout.SOUTH);
包装();
}
私有文件findFile()
{
JFileChooser chooser=新的JFileChooser();
chooser.setFileSelectionMode(仅限JFileChooser.FILES_);
//在当前目录中启动
chooser.setCurrentDirectory(新文件(“.”);
int status=chooser.showOpenDialog(null);
if(状态!=JFileChooser.APPROVE\u选项)
{
showMessageDialog(null,“未选择文件”);
抛出新的NoFileChooseException();
}
其他的
{
File File=chooser.getSelectedFile();
System.out.println(file.getName());
System.out.println(file.getPath());
返回文件;
}
}
私有类OpenButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件)
{
setListData(新对象[0]);
File=null;
尝试
{
file=findFile();
//告诉数据源要使用的文件
dataSource.createList(文件);
//JList需要一个字符串数组,请从数据源检索它
String[]data=dataSource.getData();
//数据=数据源。插入(数据);
//data=wordPair.remove(数据);
如果(数据!=null)
setListData(数据);
}
捕获(例外e)
{
}
addButton.setEnabled(真);
deleteButton.setEnabled(真);
saveButton.setEnabled(true);
wordA.setEnabled(false);
wordB.setEnabled(false);
}
}
私有类AddButtonListener实现ActionListener
{
已执行的公共无效操作(操作事件)
{
wordA.setEnabled(true);
wordB.setEnabled(true);
字符串inputStringA=wordA.getText();
字符串inputStringB=wordB.getText();
//addToList(inputStringA、inputStringB);
字符串[]data2=dataSource.getData();
//data2=dataSource.insertion(data2);
//data2=wordPair.remove(data2);
setListData(新对象[0]);
setListData(数据2);
}
}
私有类WordSelection实现ListSelect
wordPair = new WordPair();
dataSource = new SimpleWordList(wordPair); // note the parameter
class SimpleWordList {
private ArrayList<String> wordListA = new ArrayList<String>();
private ArrayList<String> wordListB = new ArrayList<String>();
private ArrayList<String> wordListC = new ArrayList<String>();
private File currentFile = null;
private WordPair wordPair;
// note the assignment constructor
public SimpleWordList(WordPair wordPair) {
this.wordPair = wordPair;
}
public String[] getData() {
String[] arr = new String[wordListA.size()];
// ****** don't create a new object -- use the one we have ***
// wordPair = new WordPair(); ****** don't create a new one!
String[] temp2 = wordPair.remove(wordListA.toArray(arr));
// use the collection class method to turn this into a
// primitive array
return temp2;
}