标签归档:Java

RSS feed of Java

一道关于Java变量初始化的面试题

首先来看笔者遇到的一道面试题,阅读下面的代码并给出执行结果:

class Singleton {
    private static Singleton singleton = new Singleton();
    public static int counter1;
    public static int counter2 = 0;

    private Singleton() {
        counter1++;
        counter2++;
    }

    public static Singleton getInstance() {
        return singleton;
    }
}

public class TestSingleton {
    public static void main(String[] args) {
        Singleton s ...

继续阅读

Java实现数组洗牌算法

数组洗牌是指将一个数组中的元素顺序打乱,随机重新排列。

算法实现思路如下:

按照下标从大到小的顺序遍历数组,记下标为i

遍历时生成范围[0, i]的随机数j,交换下标i与下标j的数组元素

参考:java.util.Collections类中的shuffle方法

Java代码:

public static void shuffle(int[] nums) {
    Random rnd = new Random();
    for (int i = nums.length - 1; i > 0; i--) {
        int j ...

继续阅读

Java OOP与Python OOP的区别

For a Java guy Python is an anarchic playgound where anyone can grab a club and start mauling your head.

对于Java的忠实粉丝来说,Python就像是一个无政府主义的游乐场,任何人都可以随手拿起一根棍子敲你的头。

For a Python guy Java is a an Orwellian universe where you are constantly shackled to someone ...

继续阅读

Python与Java的主要区别

原文链接:Python vs Java: Key Differences

Python and Java are two very different programming languages, but both can be useful tools for modern developers. If you are thinking about learning to program for the first time, then you might find ...

继续阅读

Java Swing绘制动态数据折线图

通过Java Swing可以绘制动态数据折线图。

Java Swing Line Chart

调用下面addData()方法向折线图窗口动态地追加数据,参数t为横轴时间,参数v为纵轴数值,运行效果如上图所示:

Java Swing绘制数据折线图的代码如下:


/* DataWindow.java */
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

import java.text.DateFormat;
import java.util.Date;

/**
 * Create a new window to graph the sensor readings ...

继续阅读