Programming Methodology in Java 笔记 2

Lecture 17

ArrayList  import java.util.*; (template, java 1.5之后)


boolean add(<T> element)



自己写的小例子:

package AL;
import java.util.*;




public class ArrayListTest {

ArrayList<String> slist=new ArrayList<String>();
//<T>  template  java 1.5 later
//boolean add(<T> element)
//void add(int index, <T> element)

private void slist(ArrayList<String> a) {
// TODO Auto-generated method stub
this.slist=new ArrayList<String>(a);
}

private void StringToList(String a){

StringTokenizer STL = new StringTokenizer(a);
while(STL.hasMoreTokens()){
this.slist.add(STL.nextToken());
}
}

private void addString(String a){
slist.add(a);
}

private String removeString(String a){
return slist.remove(slist.indexOf(a));
}

private void printList(){
 for(int i=0;i<this.slist.size();i++){
  System.out.print(this.slist.get(i)+" ");
 }
  System.out.println();
  System.out.println("****************************");


}
public static void main(String args[]){
ArrayListTest ALT= new ArrayListTest();
ALT.StringToList("Lord I'm doing all I can to be a better");
ALT.printList();
ALT.addString("man.");
ALT.printList();
ALT.removeString("Lord");
ALT.printList();
}
}

输出结果:

Lord I'm doing all I can to be a better 
****************************
Lord I'm doing all I can to be a better man. 
****************************
I'm doing all I can to be a better man. 
****************************


这里的ArrayList模板是使用的String。

ArrayLists holds objects。

而 int、double、boolean、char是原始类型,不是对象。


int——><Integer>

double——><Double>( before java 1.5)


java 1.5 later, boxing and unboxing

ArrayList<Integer> ilist= new ArrayList<Integer>();

int x=5;

ilist.add(x);

int z=ilist.get(0);


matrix 二维数组



Lecture 18

Array

int[][] scores = new  int[2][100];

scores[0]  is an array.


小例子:

package AL;
import java.util.*;




public class MultiArray {
private int[][] scores;
private MultiArray(){
scores=new int[2][5];
int i = scores[0].length;
}
private void initScores() {
for (int i = 0; i < scores.length; i++) {
for (int j = 0; j < scores[0].length; j++) {
scores[i][j]=1;
//System.out.print(scores[i][j]);
}
}
}

private void changeScores(int[] a) {
for (int i = 0; i < a.length; i++) {
a[i]+=1;
}
}
private void printScores(int[] b){
for (int i = 0; i < b.length; i++) {
System.out.print(b[i]);
}
System.out.println();
System.out.println("**************");

}

public static void main(String[] args) {
MultiArray MA=new MultiArray();
MA.initScores();
MA.printScores(MA.scores[0]);
MA.printScores(MA.scores[1]);
MA.changeScores(MA.scores[0]);
MA.printScores(MA.scores[0]);
MA.printScores(MA.scores[1]);
}
}



ArrayList: 

优点:动态长度、提供了更多方法。

缺点:效率低、语法复杂、   Java 5.0 之前boxing and unboxing

如果数组的长度固定,要用Array。


Debugging 调试

Design----Architect

Coding----Engineer

Testing----Vandal(破坏他人财产者)

Debugging----detective and fix problems


4D: 

Design 

Development 

Debugging 

Deployment


-----Bad Values

-----Faulty Logic

-----Unwarrant Assumptions


Use:

println :in method or show variables

unit test : 单元测试


Lecture 19 Interface

Interface——a set of methods


public class classname implements InterfaceName  

类实现接口。 接口提供给类一系列方法。类实现这些方法。


类的继承和接口的实现的区别:

接口是一种抽象 而继承有直接的关系

可以实现多个接口


Map 映射(Interface in java)

HashMap是无序的。

key and value

HashMap(Class) implements Map

is a template--->two type

HashMap<String,String> dict= new HashMap<String,String>();  or  Map<String,String> dict= new HashMap<String,String>();

HashMao<String,Integer> phonebookMap<String,String> dict= new HashMap<String,String>();


dict.put(key,value);

dict.get(word);//key    null if key not found 


写的很烂的一个样例:

import java.util.*;


public class HashTest extends EmptyHash  {
private HashMap<Integer,String> HM=new HashMap<Integer,String>();
private int key;

private void HashTest() {
Map HM=new HashMap<Integer,String>();
}

private void AddToHash(Integer Int, String str){
HM.put(Int,str);
}

private void printHash(int key) throws EmptyHash{
System.out.println(HM.get(key));
}

public static void main(String[] args) {

HashTest HT=new HashTest();
HT.AddToHash(1, "Fray");
HT.AddToHash(2, "Shi");
HT.AddToHash(3, "Qi");
HT.AddToHash(4, "Zhong");
try{
HT.printHash(2);
} catch (EmptyHash e){
if(HT.HM.get(2)==null)
e.SaySth();


}
}



Iterator  迭代器

list through set of values

ArrayList<String> names= new ArrayList<String>();

Iterator <String> it=names.iterator();

while(it.hasNext()){

print(it.next());

}


for(String name: phonebook.keySet() ){  //不需要声明迭代器。java底层去实现。JAVA 5 0 later

print;

}



Lecture 20  GUI

Graphic User Interface

Use library:

import acm

import java.awt.event.*;

import javax.swing.*;


Lecture 21

交互

public void actionPerformed( ActionEvent e {

String cmd=e.getActionCommand();

if cmd.equals.....

}


Lecture 22

Component

Container


图形界面的内容分布随着窗体大小的改变而改变。//没细听


数据解析:  

praseLine

[name albim][name band][num]


private Album parseLine(String line){

int albumNameStart = line.indexOf("[") +1;

int albumNameEnd = line.indexOf("]”);

String albumName = line.substring(albumNameStart,albumNameEnd);

int bandNameStart = line.indexOf("[",albumNameEnd+1) + 1;

....


return ( new Album( albumName, bandName, numStocked ));


}


Lecture 23 

Searching and Sorting

Binary Search: 有序数组


private int binarySearch(int key) {

int lh=0;

int rh=disp.length()-1;

while(lh<=rh){

int mid=(lh+rh)/2;

if (disp.get(mid)==key) return mid;

if(disp.get(mid) < key)

lh=mid + 1;\

if(disp.get(mid)>key)

rh=mid - 1; 

}

return -1;

}


radix sort 从最低位到最高位依次排序。


Lecture 24  Data Structure

大型数据

classes

methods

unique id


collection 集合----ArrayList/HashMap

Methods:

boolean add(<T> value)

void clear()

boolean contains(<T> value)

Iterator iterator()


FlyTunes:

add  Songs

add  Albums


class Song{

String name,band;

double price;

unique identifier;

}



Shallow Copy: Only one copy.


代码有bug,调试中。


Lecture 25 

Build a Social Network


Concurrentcy:并发性

Thread

public class MyClass implements Runnable{


public MyClass(){

}

public void run(){

}

}


MyClass x=new MyClass();

Thread t=new Thread(x);

t.start();


Lecture 26 Standard Java

打成Jar包:(可以存储数据)

Manifest-Version:1.0

Main-Class:

Class-Path: am


Lecture 27 A broad view of CS

Lecture 28 Project展示、考点点拨


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值