您的位置 首页 java

Java编程中有哪些很酷的技巧?

一、标签循环:

 package com.apy29;

public class Snippet {
public  static   Void  main(String[] args) {
int arr[] = new int[] { 12, 58, 64, 21, 36, 84, 56, 91 };
System.out.println("开始。");
outer:
for (int i = 0; i < arr.length; i++) {
System.out.println(i);
if (arr[i] == 36) {
break outer;
}
}
System.out.println("结束。");
}
}
  

二、Class类型的定义可以放在任何地方:

 package com.apy29;
class Test {
public static void main(String[] args) {
class InnerClass {
void way() {
System.out.println("way");
}
}
new InnerClass().way();
}
}
  

三、有一个对象叫做Void:

Void (Java Platform SE 7 ) 常见的用例是不需要返回值的泛型方法:

Modifier and TYPE

Field and Description

static Class < Void >

TYPE

The Class object representing the pseudo-type corresponding to the keyword void.

四、 new ArrayList<Object>() 允许你在没有Arrays. asList的情况下声明和初始化:

  private  static List<String> places = new ArrayList<String>() {
{
add("x");
add("y");
}
};  

五、 静态方法 是可以通用的:

 package com.apy29;
class Test {

static <T> T identity(T t) {
return t;
}

public static void main(String[] args) {
 String  s = Test.<String> identity("Hi");
System.out.print(s);
}
}  

六、你可以提供可变长度的参数:

 package com.apy29;

public class Snippet2 {
void example(String... strings) {
for (int i = 0; i < strings.length; i++) {
String string = strings[i];
System.out.println(string);
}
};

public static void main(String[] args) {
String[] strings = new String[] { "hello world", "你好,世界。", "我来自新世界。" };
new Snippet2().example(strings);
}
}  

七、 泛型 类型参数中的联合

 package com.apy29;

public class Baz<T  extends  Foo & Bar> {

}

interface Foo {

}

interface Bar {

}
  

八、同时定义、实例化和调用所有内容:

 package com.apy29;

public class Snippet {
public static void main(String[] args) {
new Object() {
void hi(String in) {
System.out.println(in);
}
}.hi("weird");
}
}  

九、将 Java int 数组转化为 integer 数组对象:

 package com.apy29;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class IntArrConvertIntegerList {

private static List<String> places = new ArrayList<String>() {
{
add("x");
add("y");
}
};

/**
* 将arrint 转化为 java Integer数组对象
*
*/public void intArrConvertIntegerList(int[] arrint) {
IntStream stream = Arrays.stream(arrint);
Stream<Integer> boxed = stream.boxed();
 Integer [] array = boxed.toArray(Integer[]::new);
for (int i = 0; i < array.length; i++) {
Integer integer = array[i];
System.out.println(integer);
}
}

public static void main(String[] args) {
Integer arr[] = new Integer[] { 12, 58, 64, 21, 36, 84, 56, 91 };
int arrint[] = new int[] { 12, 58, 64, 21, 36, 84, 56, 91 };

// Arrays.asList() 方式将数组转化为List
List<Integer> asList = Arrays.asList(arr);
for (int i = 0; i < asList.size(); i++) {
int j = asList.get(i);
System.out.println(j);
}

// new ArrayList<String>(){add("x");add("y")} 直接定义
for (int i = 0; i < places.size(); i++) {
System.out.println(places.get(i));
}

// 转化示例
new IntArrConvertIntegerList().intArrConvertIntegerList(arrint);
}
}
  

这是我在使用Java的过程中总结的一些Java编程的技巧,相信会对你有帮助,后续我会继续探索并带给大家更多的Java知识点。

文章来源:智云一二三科技

文章标题:Java编程中有哪些很酷的技巧?

文章地址:https://www.zhihuclub.com/175434.shtml

关于作者: 智云科技

热门文章

网站地图