java 小技巧

一、函数返回值
1、return操作,如果返回值为多个,寻找或者构造一个数据结构带回去
2、使用如下方法,用入参带回来

TestKNN t = new TestKNN();
String datafile = new File("").getAbsolutePath() + File.separator +"resource"+File.separator + "datafile.data";
		try {
			List<List<Double>> datas = new ArrayList<List<Double>>();
			t.read(datas, datafile);
			...
			}
public void read(List<List<Double>> datas, String path){
		try {
			BufferedReader br = new BufferedReader(new FileReader(new File(path)));
			String data = br.readLine();
			List<Double> l = null;
			while (data != null) {
				String t[] = data.split(" ");
				l = new ArrayList<Double>();
				for (int i = 0; i < t.length; i++) {
					l.add(Double.parseDouble(t[i]));
				}
				datas.add(l);
				data = br.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

二、子类继承父类

public static final GraphDatabaseConnector dbConnector = new Neo4jConnector(driver,session);

当调用Neo4jConnector中的方法,而GraphDatabaseConnector 中没有的时候,这样是无法调用到的

三、实体类中的toString方法

public String toString() {
		return new Gson().toJson(this);
	}

这样不容易有空指针异常

四、循环中需要赋值给数组,这时要用到循环的变量i,相当于指针,但是如果不是普通的循环,就不能这样做,

for (int i = 0; i < arr2Row; i++) {
        	List<Float> arr2list=new ArrayList<>();
	        for (int j = i*col; j < (i+1)*col; ++j) {
	        	
	        	arr2list.add(mat2Buf.get(j));
	        	System.out.println("被广播出去的index:" +i+ ",element:" + mat2Buf.get(j));
	        }
	        map2.put(i,  ArrayUtils.toPrimitive(arr2list.toArray(new Float[0]), 0.0F));
	        arr2list=null;
        }

五、指针的问题

while (f.hasNext) {
          var ele=f.next()
        	buf2.appendAll(ele._2)
        	buf1.+=(ele._1)
        }

一定要把f.next()赋值,否则连续用两次,第二次用的时候,其实指针已经走了

六、list去重

List<string> listWithoutDup = new ArrayList<>(new HashSet<>(listWithDup));
假如传入的是对象,需要重写equals和hashcode方法

七、Java中List, Integer[], int[]的相互转换
https://blog.csdn.net/zx000003/article/details/82691578

八、Collections.singletonList(something)是不可变的,
对Collections.singletonList(something)返回的列表所做的任何更改将导致UnsupportedOperationException 。
Arrays.asList(something)允许Arrays.asList(something) 更改 。此外,由Collections.singletonList(something)返回的List的容量将始终为1,而Arrays.asList(something)的容量将为已支持数组的大小。

九、list大小

result=new ArrayList<List<String>>(max);
		for (int i = 0; i < result.size(); i++) {
			List<String> row=new ArrayList<String>(max);
			for (int j = 0; j < row.size(); j++) {
				row.add(".");
			}
			
			result.add(row);
		}
		
		System.out.println(result);

上边的结果,即使初始化了max的值,result是空的

要想初始化result,必须如下方式

result=new ArrayList<List<String>>(max);
		for (int i = 0; i < max; i++) {
			List<String> row=new ArrayList<String>(max);
			for (int j = 0; j < max; j++) {
				row.add(".");
			}
			
			result.add(row);
		}
		
		System.out.println(result);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1,设置面板不可拖动变化 java.awt.Frame.setResizable(false); 2,设置一组单选RadioButton 先声明: ButtonGroup jButtonGroup = new ButtonGroup(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); JRadioButton jRadioButton3 = new JRadioButton(); 后捆绑: jButtonGroup.add(jRadioButton1); jButtonGroup.add(jRadioButton2); jButtonGroup.add(jRadioButton3); 3,弹出一个提示框 JOptionPane.showMessageDialog(this,) 4,获取一个列表对象的总长度 getItemCount(); 5,判断一个列表对象中一个Item是否被选中 isIndexSelected(int index); 1,Jtable加到JScroll中,默认只实现了垂直滚动(VERTICAL),而水平滚动(Horizontal)没有实现,这时候会使得无论格子数量,总长度=界面宽度。 解决:jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 2,Swing常常显示中文乱码,对于使用了UIManager.getSystemLookAndFeelClassName() 的程序,可以采用如下方法; 解决:在UIManager.getSystemLookAndFeelClassName() 下方添加代码 java.util.Enumeration enum = UIManager.getDefaults().keys(); String str; while(enum.hasMoreElements()){ if((str = (String)enum.nextElement()).endsWith("font")){ //只要与字体相关 UIManager.put(str,new Font("宋体",0,12)); } } 3,改变菜单栏等大小 设置合适的字体 4,对于En_US编码的informix,JTable中文显示乱码 对于这类情况,JDBC在取出数据而未显示时,已经是乱码,解决方法: 在连接语句的最后,加上;NEWCODESET=gbk,8859-1,819 5, 1、JFrame在屏幕中居中显示,只须在主类的构造方法里面加上一句: setLocationRelativeTo(null); 2、若要让JDialog居中显示,可以加上一句: JDialogname.setLocationRelativeTo(null); setLocationRelativeTo()方法一定要JDialog的SetSize()方法或者是pack()的后面,

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值