您的位置:首页 > 编程语言 > Java开发

关于一些JAVA的基础知识总结

2016-08-28 01:02 751 查看

Java 基础

有如下两个数组 int a[] = {1,3,5,7,8}; int b[] = {0,3,5,10,20}; 选出属于数组b但不属于数组a的元素

public class TestInt {

public static void main(String[] args) {
int a[] = {1,3,5,7,8};
int b[] = {0,3,5,10,20};
System.out.println(Arrays.toString(find(b,a)));
}

//选出属于数组a但不属于数组b的元素
public static int[] find(int[] a, int[] b){
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < a.length; ++i) {
boolean bContained = false;
for(int j = 0; j < b.length; ++j) {
if (a[i] == b[j]) {
bContained = true;
break;
}
}
if (!bContained) {
list.add(a[i]);
}
}
int res[] = new int[list.size()];
for(int i = 0; i < list.size(); ++i){
res[i] = list.get(i);
}
return res;
}
}


继承、静态的理解

public class TestStaticExtend {
public static void main(String[] args) {
A se = new B();
se.print();
}
}

class A{
static {
System.out.println("AAAA");
}

public A(){
System.out.println("struc A");
}

void print(){
System.out.println("this is class A");
}
}
class B extends A{
static{
System.out.println("BBBB");
}

public B(){
System.out.println("struc B");
}

void print(){
System.out.println("this is class B");
}
}


结果:

AAAA

BBBB

struc A

struc B

this is class B

String 的理解

public class TestString {
String str = new String("hello world");
String[] strarr = {"hello world"};
char[] ch = { 'a', 'b', 'c' };

static TestString ex = new TestString();
public static void main(String[] args) {

change(ex.str, ex.ch);
System.out.println(ex.getStr()+ " and " + Arrays.toString(ex.ch));

change(ex.strarr, ex.ch);
System.out.println(ex.strarr[0] + " and " + Arrays.toString(ex.ch));
}

public static void change(String[] str, char[] ch) {
str[0] = "test ok";
ch[0] = 'g';
}

public static void change(String str, char[] ch) {
ex.setStr("test ok");
ch[0] = 'g';
}

public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}


结果:

test ok and [g, b, c]

test ok and [g, b, c]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java string