十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
1.java内存模型简称JMM
成都创新互联专注于企业营销型网站建设、网站重做改版、新河网站定制设计、自适应品牌网站建设、H5网站设计、商城网站开发、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为新河等各大城市提供网站开发制作服务。
2.通俗点讲就是指你代码中写的那些内容在内存中是怎么存储的
3.比如说 Student stu = new Student();int a = 5; 你新建了一个学生类,建立了一个变量a,这些内容在内存中是咋存储的,
4.大的来说JMM分为堆栈,一般情况下学生类分配在堆,变量a分配在栈,
原因是你在创建Card[]数组时,里面的数组元素没有初始化,全部默认为null类型,因此报错。如果你把赋值放到构造方法里面,会省掉不少工序。
class Cards {
private String color;
private String value;
public Cards(String col,String val){
color=col;
value=val;
}
public void showcard() {
System.out.println(color+"\t"+value);
}
public static void main(String args[]) {
String nums[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String cols[]={"hearts","spade","clubs","diamonds"};
Cards cards[]=new Cards[54];
for(int i = 0; i cols.length;i++){
for(int j = 0;j nums.length;j++){
cards[(i * cols.length + j)] = new Cards(cols[i],nums[j]);
cards[(i * cols.length + j)].showcard();
}
}
cards[52] = new Cards("big","king");
cards[52].showcard();
cards[53] = new Cards("little","king");
cards[53].showcard();
}
}
概述
java程序输入需要用到Scanner工具类,输出则使用println
解析
1、输入:
从键盘输入需要用到java的Scanner这个util包下的工具类
Scanner中输入是需要对输入的字符进行分类,int类型为nextint()方法,double类型为nextDouble()方法,字符串类型为next()方法。
#code:
#out:
2、输出:
java常用的输出为println
#code:
#out:
除了println()方法之外,java还有print()和printf()两种输出方法,
print、println、printf的区别:
print--是函数,可以返回一个值,只能有一个参数。
println 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始。
printf--函数,把文字格式化以后输出,直接调用系统调用进行IO的,他是非缓冲的。
拓展内容
java 输入输出流(I/O)
Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列。Java的I/O流提供了读写数据的标准方法。任何Java中表示数据源的对象都会提供以数据流的方式读写它的数据的方法。
Java.io是大多数面向数据流的输入/输出类的主要软件包。此外,Java也对块传输提供支持,在核心库 java.nio中采用的便是块IO。
流IO的好处是简单易用,缺点是效率较低。块IO效率很高,但编程比较复杂。
Java IO模型 :
Java的IO模型设计非常优秀,它使用Decorator模式,按功能划分Stream,您可以动态装配这些Stream,以便获得您需要的功能。例如,您需要一个具有缓冲的文件输入流,则应当组合使用FileInputStream和BufferedInputStream。
输入流(Input Stream):
程序从输入流读取数据源。数据源包括外界(键盘、文件、网络…),即是将数据源读入到程序的通信通道。
输出流(output Stream):
程序向输出流写入数据。将程序中的数据输出到外界(显示器、打印机、文件、网络…)的通信通道。
就是代码和数据啊
用程序控制线程 程序就是代码
重点放在CPU上是 因为 CPU执行线程 与堆内存密切 线程等待和异步同步执行都是需要掌握的 加上线程锁的问题 CPU处理机制就难以理解了
DefaultTableModel是常用的java表格模型。
参考代码:
package com.dusd;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Arrays;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
/**
* 表格模型事件示例
* p
* liA component generally gains the focus when the user clicks it,
* or when the user tabs between components, or otherwise interacts
* with a component. A component can also be given the focus programmatically,
* such as when its containing frame or dialog-box is made visible.
* The snippet of the codes below shows how to give a particular component
* the focus every time the window gains the focus.
*
* @author HAN
*
*/
public class TableModel extends JFrame {
/**
*
*/
private static final long serialVersionUID = -8581492063632813033L;
public TableModel() {
// TODO Auto-generated constructor stub
final Container container = getContentPane();
VectorString tableColumnNames = new VectorString();
tableColumnNames.add("A");
tableColumnNames.add("B");
VectorVectorString tableValues = new VectorVectorString();
for (int i = 1; i 5; i++) {
VectorString vector = new VectorString();
vector.add("A" + i);
vector.add("B" + i);
tableValues.add(vector);
}
final DefaultTableModel defaultTableModel = new DefaultTableModel(
tableValues, tableColumnNames);
final JTable table = new JTable(defaultTableModel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(table);
container.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
container.add(panel, BorderLayout.SOUTH);
JLabel labelA = new JLabel("A: ");
final JTextField textFieldA = new JTextField(15);
JLabel labelB = new JLabel("B: ");
final JTextField textFieldB = new JTextField(15);
JButton buttonAdd = new JButton("添加");
JButton buttonDel = new JButton("删除");
JButton buttonDeselected = new JButton("取消选择");
panel.add(labelA);
panel.add(textFieldA);
panel.add(labelB);
panel.add(textFieldB);
panel.add(buttonAdd);
panel.add(buttonDel);
panel.add(buttonDeselected);
buttonAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int[] selectedRows = table.getSelectedRows(); // table
// 默认情况容许多行选择
VectorString rowData = new VectorString();
rowData.add(textFieldA.getText());
rowData.add(textFieldB.getText());
if (selectedRows.length == 0) {
defaultTableModel.addRow(rowData);
textFieldA.setText(null);
textFieldB.setText(null);
} else if (selectedRows.length == 1) {
// System.out.println(selectedRows[0]);
defaultTableModel.insertRow(selectedRows[0] + 1, rowData);
textFieldA.setText(null);
textFieldB.setText(null);
} else {
JOptionPane.showMessageDialog(container,
"Your operation is forbidden", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
});
buttonDel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int[] selectedRows = table.getSelectedRows(); // table
// 默认情况容许多行选择
for (int i = 0; i selectedRows.length; i++) {
// System.out.println(selectedRows[i]);
defaultTableModel.removeRow(selectedRows[i] - i);
}
}
});
buttonDeselected.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
table.clearSelection();
}
});
scrollPane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// System.out.println("here");
if (e.getClickCount() == 1
e.getButton() == MouseEvent.BUTTON1) {
table.clearSelection();
}
}
});
// make the text field focused every time the window is activated
addWindowFocusListener(new WindowAdapter() {
@Override
public void windowGainedFocus(WindowEvent e) {
// TODO Auto-generated method stub
textFieldA.requestFocus();
}
});
// **************************************************************
// This is a standard snippet to realize the desired column selection as in
// Excel
// **************************************************************
// row selection mode
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// column selection mode
TableColumnModel tableColumnModel = table.getColumnModel();
tableColumnModel.getSelectionModel().setSelectionMode(
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
// allow the column selection (the row selection is allowed by default)
table.setColumnSelectionAllowed(true);
final JTableHeader tableHeader = table.getTableHeader();
tableHeader.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
// Extended modifiers represent the state of all modal keys,
// such as ALT, CTRL, META.
if (e.getClickCount() == 1
e.getButton() == MouseEvent.BUTTON1) {
// Point point = new Point(e.getX(), e.getY());
Point point = new Point(e.getPoint());
int columnNum = tableHeader.columnAtPoint(point);
// System.out.println(columnNum);
int[] selectedColumns = table.getSelectedColumns();
if (selectedColumns.length != 0) {
// System.out.println("here1");
// System.out.println(InputEvent.getModifiersExText(e.getModifiersEx()));
if (e.getModifiersEx() == (InputEvent.CTRL_DOWN_MASK)) {
// System.out.println("ctrl");
if (Arrays.binarySearch(selectedColumns, columnNum) = 0) {
table.removeColumnSelectionInterval(columnNum,
columnNum);
} else {
table.addColumnSelectionInterval(columnNum,
columnNum);
}
} else if (e.getModifiersEx() == (InputEvent.SHIFT_DOWN_MASK)) {
// System.out.println("shift");
table.setColumnSelectionInterval(
selectedColumns[0], columnNum);
} else {
table.setColumnSelectionInterval(columnNum,
columnNum);
}
} else {
// System.out.println("here2");
table.setColumnSelectionInterval(columnNum, columnNum);
}
table.setRowSelectionInterval(0, table.getRowCount() - 1);
}
}
});
defaultTableModel.addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
// TODO Auto-generated method stub
int type = e.getType();
int firstRow = e.getFirstRow();
// int lastRow = e.getLastRow(); // the last row seems to be always equal to the first row
int column = e.getColumn();
switch (type) {
case TableModelEvent.DELETE:
System.out.print("此次事件由 删除 行触发:");
System.out.println("此次删除的是第 " + firstRow + " 行");
break;
case TableModelEvent.INSERT:
System.out.print("此次事件由 插入 行触发:");
System.out.println("此次插入的是第 " + firstRow + " 行");
break;
case TableModelEvent.UPDATE:
System.out.print("此次事件由 更新 行触发:");
System.out.println("此次更新的是第 " + firstRow + " 行第 " + column + " 列");
break;
default:
System.out.println("此次事件由 其他原因 触发");
}
}
});
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TableModel frame = new TableModel();
frame.setTitle("表格模型事件示例");
frame.pack(); //Realize the components.
// frame.setBounds(100, 100, 600, 300);
// textFieldA.requestFocus();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); //Display the window.
}
}