「java数据结构设计」java数据结构设计怎么写

博主:adminadmin 2022-11-27 11:41:09 52

今天给各位分享java数据结构设计的知识,其中也会对java数据结构设计怎么写进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA数据结构有哪几种?

数组、栈 、队列、链表、树、堆 、图、散列表 。

1:数组是计算机编程语言上,对于“Array”的中文称呼,是用于储存多个相同类型数据的集合。

2:栈是限定仅在表尾进行插入和删除操作的线性表,栈者,存储货物或供旅客住宿的地方,可引申为仓库、中转站,引入到计算机领域里,就是指数据暂时存储的地方,所以才有进栈、出栈的说法。

3:一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。

4:链表,一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。

5:哈希表,是根据关键码值而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。

数据结构(java版)哈希表的设计

1.什么是哈希表?

哈希表是一种数据结构,它提供了快速的插入操作和查找操作。其基于数组来实现。

2.哈希化

1)直接将关键字作为索引。

2)将单词转换成索引。

1将字母转换成ASCII码,然后进行相加

2幂的连乘

3压缩可选值

3.压缩后仍然可能出现的问题。

冲突:不能保证每个单词都映射到数组的空白单元。

解决办法:

1开放地址法

2链地址法

/**

* 员工信息类

* @author Administrator

*

*/

public class Info {

private String key;

private String name;

public Info(String key, String name) {

this.key = key;

this.name = name;

}

public String getKey() {

return key;

}

public void setKey(String key) {

this.key = key;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

import java.math.BigInteger;

public class HashTable {

private Info[] arr;

/**

* 默认的构造方法

*/

public HashTable() {

arr = new Info[100];

}

/**

* 指定数组初始化大小

*/

public HashTable(int maxSize) {

arr = new Info[maxSize];

}

/**

* 插入数据

*/

public void insert(Info info) {

arr[hashCode(info.getKey())] = info;

}

/**

* 查找数据

*/

public Info find(String key) {

return arr[hashCode(key)];

}

public int hashCode(String key) {

// int hashVal = 0;

// for(int i = key.length() - 1; i = 0; i--) {

// int letter = key.charAt(i) - 96;

// hashVal += letter;

// }

// return hashVal;

BigInteger hashVal = new BigInteger("0");

BigInteger pow27 = new BigInteger("1");

for(int i = key.length() - 1; i = 0; i--) {

int letter = key.charAt(i) - 96;

BigInteger letterB = new BigInteger(String.valueOf(letter));

hashVal = hashVal.add(letterB.multiply(pow27));

pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));

}

return hashVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();

}

}

public class TestHashTable {

public static void main(String[] args) {

HashTable ht = new HashTable();

ht.insert(new Info("a","张三"));

ht.insert(new Info("ct","李四"));

ht.insert(new Info("wangwu","王五"));

System.out.println(ht.find("a").getName());

System.out.println(ht.find("ct").getName());

}

}

JAVA数据结构课程设计,航空订票系统求助

今天比较闲, 编了一套,一共4个 class。不懂再问。

这格式真要命,凑活看吧。

public enum AirClass{

 FIRST,

 SECOND,

 THIRD

}

--------------------------------------------

public class Customer{ 

    private String name; 

    private int ticketNumber; 

    private AirClass airClass;  

    public Customer(String name, int ticketNumber, AirClass airClass){  

        this.name = name;  

        this.ticketNumber = ticketNumber;  

        this.airClass = airClass; 

   }

   public String getName() {  return name; }

   public void setName(String name) {  this.name = name; }

   public int getTicketNumber() {  return ticketNumber; }

   public AirClass getAirClass() {  return airClass; }

}

------------------------------------------------

import java.util.LinkedList;

import java.util.List;

public class AirLine { 

    private String destination; 

    private String flightName; 

    private String flightNumber; 

    private int dayOfWeek; 

    private int capacity; 

    private int remainder; 

    private ListCustomer booking = new LinkedListCustomer(); 

    private ListCustomer preBooking = new LinkedListCustomer();

 

    public AirLine(String destination, String flightName, String flightNumber,   int dayOfWeek, int capacity) {  

        this.destination = destination;  

        this.flightName = flightName;  

        this.flightNumber = flightNumber;  

        this.dayOfWeek = dayOfWeek;  

        this.capacity = capacity;  

        this.remainder = capacity; }

 public String getDestination() {  return destination; }

 public String getFlightName() {  return flightName; }

 public String getFlightNumber() {  return flightNumber; }

 public int getDayOfWeek() {  return dayOfWeek; }

 public int getCapacity() {  return capacity; }

 public int getRemainder() {  return remainder; }

 public ListCustomer getBooking() {  return booking; }

 public ListCustomer getPreBooking() {  return preBooking; }

 

 public boolean addBooking(Customer c) {  

     if (this.remainder = c.getTicketNumber()) {   

         this.remainder = this.remainder - c.getTicketNumber();   

         this.booking.add(c);   

         return true;  

     } else {   

         this.preBooking.add(c);   

         return false;  

     } 

  }

 

 public void removeBooking(Customer c) {  

     if (this.booking.contains(c)) {   

         this.booking.remove(c);   

         this.remainder = this.remainder + c.getTicketNumber();  

     } else {   

         throw new IllegalArgumentException("Customer not found.");  

     } 

  }

 

 public void removePreBooking(Customer c) {  

     if (this.preBooking.contains(c)) {   

         this.preBooking.remove(c);  

     } else {   

         throw new IllegalArgumentException("Customer not found.");  

     } 

  }

 

 public String toString() {  

     return this.flightName + ": " + this.flightNumber + ": "    

         + this.dayOfWeek + ": " + this.remainder; 

 }

}

---------------------------------

import java.util.Calendar;import java.util.Date;

import java.util.LinkedList;import java.util.List;

import java.util.concurrent.TimeUnit;

public class Main { 

    private ListAirLine airLines = new LinkedListAirLine();

 

     private void init() {  

         final AirLine airLine1 = new AirLine("beijing", "flightName",    "flightNumber", Calendar.MONDAY, 300);  

         this.airLines.add(airLine1);  

         // 自己加新的 airLine2, airLine3 ...

  

      final Customer customer1 = new Customer("Tom", 2, AirClass.FIRST);  

      airLine1.addBooking(customer1);  

      // 自己加新的 customer2, customer3 ... 

    }

    

 public AirLine search(String destination) {

      if (destination == null)   return null;

      final int currenDayOfWeek = Calendar.getInstance().get(    Calendar.DAY_OF_WEEK);

      AirLine res = null;  

      int diffToCurrenDate = 7;  

      for (AirLine a : airLines) {   

          if (destination.equals(destination)) {    

              if (res == null) {     

                  res = a;     

                  continue;    

           }

        int diff = a.getDayOfWeek() - currenDayOfWeek;    

        if (diff  0)     diff = diff + 7;

        if (diff  diffToCurrenDate) {     

            diffToCurrenDate = diff;     

            res = a;    

         }   

      }  

    }  

    if (res == null) {   

        System.out.println("Not found");  

    } else {   

        System.out.println("日期 "     + new Date(System.currentTimeMillis()       + TimeUnit.DAYS.toMillis(diffToCurrenDate)) + " : "     + res.toString());  

       }  

      return res; 

    }

 

 public void booking(AirLine a, Customer c) {  

     if (a.addBooking(c)) {   

         System.out.println("订到了");  

     } else {   

         System.out.println("排队中");  

       } 

     }

     

 public void cancel(AirLine a, Customer c) {  

     a.removeBooking(c);  

     for (Customer pre : a.getPreBooking()) {   

         if (a.addBooking(pre)) {    

             a.removePreBooking(pre);    

             System.out.println("排队的订到了");    

             break;   

          } else {    

              System.out.println("票余量不够当前面排队的人, 下一个");   

          }  

       } 

    }

}

设计一种java数据结构,按照权重进行排序

public enum SortBy {

Number,

Weights1,

Weights2

}

import java.util.Comparator;

 

public class MyComparator implements ComparatorStudent{

 

private SortBy sortBy;

 

public MyComparator (SortBy sortBy){

this.sortBy = sortBy;

}

 

@Override

public int compare(Student s1, Student s2) {

switch (this.sortBy) {

case Weights1:

return s1.getWeights1() - s2.getWeights1();

case Weights2:

return s1.getWeights2() - s2.getWeights2();

default:

return s1.getNumber() - s2.getNumber();

}

}

}

import java.util.Collections;

import java.util.Comparator;

import java.util.LinkedList;

import java.util.List;

 

public class Student {

private int number;

private int weights1;

private int weights2;

 

public Student(int number, int weights1, int weights2) {

this.number = number;

this.weights1 = weights1;

this.weights2 = weights2;

}

 

public int getNumber() {

return this.number;

}

 

public int getWeights1() {

return this.weights1;

}

 

public int getWeights2() {

return this.weights2;

}

 

public static void main(String[] args){

final Student s1 = new Student(1, 20, 300);

final Student s2 = new Student(2, 10, 100);

final Student s3 = new Student(3, 30, 200);

 

final ListStudent array = new LinkedListStudent();

array.add(s1);

array.add(s2);

array.add(s3);

 

 

final ComparatorStudent numberComparator = new MyComparator(SortBy.Number);

Collections.sort(array, numberComparator);

System.out.println("sort by number");

System.out.print("Number: ");

for(Student s: array){

System.out.print(s.getNumber() + " ");

}

System.out.println("\n\n");

 

 

final ComparatorStudent weights1Comparator1 = new MyComparator(SortBy.Weights1);

System.out.println("sort by weights1");

System.out.print("Number: ");

Collections.sort(array, weights1Comparator1);

for(Student s: array){

System.out.print(s.getNumber() + " ");

}

System.out.println("\n\n");

 

final ComparatorStudent weights2Comparator = new MyComparator(SortBy.Weights2);

System.out.println("sort by weights2");

System.out.print("Number: ");

Collections.sort(array, weights2Comparator);

for(Student s: array){

System.out.print(s.getNumber() + " ");

}

System.out.println("\n\n");

}

}

sort by number

Number: 1 2 3

 

 

sort by weights1

Number: 2 1 3

 

 

sort by weights2

Number: 2 3 1

java八大数据结构要学多久

2个月。因为运行环境需要一天,关键词需要一天,数据类型、运算符需要一天,条件结构、循环结构给你一周,方法重载、数组给你一周。面向对象、这个、构造方法,差不多一周就够了。常用API、继承与多态、聚合、异常、多线程、网络编程常用的API给你一天,继承和多态给你一天,聚集给你一个星期,多线程(入门)、网络编程给你一个星期,所以大概要2个月左右。

JAVA数据结构有哪几种

JAVA数据结构有以下几种:

1、List:

List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置。用户能够使用索引(元素在List中的位置,类似于数组下 标)来访问List中的元素,这类似于Java的数组。

2、Vector:

基于数组(Array)的List,其实就是封装了数组所不具备的一些功能方便我们使用,所以它难易避免数组的限制,同时性能也不可能超越数组。

另外很重要的一点就是Vector是线程同步的(sychronized)的,这也是Vector和ArrayList 的一个的重要区别。

3、ArrayList:

同Vector一样是一个基于数组上的链表,但是不同的是ArrayList不是同步的。所以在性能上要比Vector好一些,但是当运行到多线程环境中时,可需要自己在管理线程的同步问题。

4、LinkedList:

LinkedList不同于前面两种List,它不是基于数组的,所以不受数组性能的限制。 它每一个节点(Node)都包含两方面的内容:节点本身的数据(data),下一个节点的信息(nextNode)。

所以当对LinkedList做添加,删除动作的时候就不用像基于数组的ArrayList一样,必须进行大量的数据移动。只要更改nextNode的相关信息就可以实现了,这是LinkedList的优势。

5、HashSet:

虽然Set同List都实现了Collection接口,但是他们的实现方式却大不一样。List基本上都是以Array为基础。

但是Set则是在 HashMap的基础上来实现的,这就是Set和List的根本区别。HashSet的存储方式是把HashMap中的Key作为Set的对应存储项。

6、HashMap:

基于哈希表的 Map 接口的实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。(除了不同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。

7、HashTable:

Hashtable 是一个散列表,它存储的内容是键值对(key-value)映射。Hashtable 继承于Dictionary,实现了Map、Cloneable、java.io.Serializable接口。

Hashtable 的函数都是同步的,这意味着它是线程安全的。它的key、value都不可以为nul

java数据结构设计的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java数据结构设计怎么写、java数据结构设计的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。