20162321王彪 2017-2018《程序设计与数据结构》第五周学习总结

本文主要是介绍20162321王彪 2017-2018《程序设计与数据结构》第五周学习总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

学习目标

  • 掌握Java Collections API的基本结构
  • 理解Collection的抽象设计
  • 掌握Collection实现的Java相关技术:继承/多态/泛型/接口
  • 掌握栈的应用
  • 掌握栈的实现(数组,链式)
  • 分析Java Stack API

知识点总结

  • 集合(collection):线性的和非线性的;线性集合(linear collection)是集合中的元素排成一行。非线性集合是按不同于一行的方式来组织元素,例如按层次或是按网络的方式。
  • 抽象(abstraction)在某些时候隐藏其细节。抽象数据类型(abstract data type,ADT)是其值和操作都没有在程序设计语言中定义的一种数据类型。它是抽象的,因为实现细节必须要定义,而且要对用户隐藏。

代码实现

  • 用数组实现栈
public class ArrayStack<T> {private final int DEFAULT_CAPACITY = 10;private int count;private T[] stack;private T item;public T pop () {if (count==0){System.out.println("Wrong num");}else {item = stack[count-1];stack[count-1]=null;}return item;}public T peek(){if (count==0){System.out.println("Wrong num");}else {item = stack[count-1];}return item;}public boolean isEmpty(){if (count==0)return true;elsereturn false;}public int size(){return count;}public ArrayStack(){count=0;stack =(T[])(new Object[DEFAULT_CAPACITY]);}public void push(T element){if (count==stack.length)expandCapacity();stack[count]=element;count++;}public String toString(){String result = "<top of stack>";for (int index=count-1;index>=0;index--)result += stack[index]+"\n";return result+"<bottom of stack>";}private void expandCapacity(){T[] larger = (T[])(new Object[stack.length*2]);for (int index=0;index<stack.length;index++){larger[index] = stack[index];}stack = larger;}
  • peek()操作要注意当前栈是否是空。

  • 用链表实现栈

public class LinkedStack<T> implements Stack<T>
{private int count;private LinearNode<T> top = new LinearNode<T>(null);//表头,数据为空private LinearNode<T> end = new LinearNode<T>(null);//表尾//----------------------------------------------------------------//  Creates an empty stack using the default capacity.// ----------------------------------------------------------------public LinkedStack() {top.setNext(end);count=0;}
//----------------------------------------------------------------
//  Removes the element at the top of this stack and returns a
//  reference to it. Throws an EmptyCollectionException if the
//  stack contains no elements.
// ----------------------------------------------------------------
public T pop() throws EmptyCollectionException {if (count == 0) throw new EmptyCollectionException ("Pop operation failed. " + "The stack is empty.");T result = top.getNext().getElement();top.setNext(top.getNext().getNext());top.setElement(null);count--;
return result;
}
//----------------------------------------------------------------//   Returns a string representation of this stack.
// ----------------------------------------------------------------public String toString() {String result = "<top of stack>\n";LinearNode current = top;while (current != null) {result += current.getElement() + "\n";current = current.getNext();}return result + "<bottom of stack>";}//----------------------------------------------------------------
//  The following methods are left as programming projects.
// ----------------------------------------------------------------@Overridepublic int hashCode() {return super.hashCode();}@Overridepublic void push(T element) {LinearNode<T> now = new LinearNode<T>(element);//now是用来保存加入元素的节点类的实例now.setNext(top.getNext());//指向top指向的对象top.setNext(now);count++;}@Overridepublic T peek() {return top.getNext().getElement();}@Overridepublic boolean isEmpty() {if (count==0)return true;elsereturn false;}@Overridepublic int size() {return count;}public static void main(String[] args) {LinkedStack mLinked = new LinkedStack();mLinked.push(1);System.out.println(mLinked.isEmpty());}}
//************************************************************
//  LinearNode.java       Java Foundations
// //  Represents a node in a linked list.
// ************************************************************class LinearNode<T> {private LinearNode<T> next;private T element;
//----------------------------------------------------------------
//  Creates an empty node.
// ----------------------------------------------------------------
public LinearNode() {next = null;element = null; }
//----------------------------------------------------------------
//  Creates a node storing the specified element.
// ---------------------------------------------------------------
public LinearNode (T elem) {next = null;element = elem; }
//----------------------------------------------------------------
//  Returns the node that follows this one.
// ----------------------------------------------------------------
public LinearNode<T> getNext() {return next;}
//----------------------------------------------------------------
//  Sets the node that follows this one.
// ----------------------------------------------------------------
public void setNext (LinearNode<T> node) {next = node;}
//----------------------------------------------------------------
//  Returns the element stored in this node.
// ----------------------------------------------------------------
public T getElement() {return element;}
public void setElement (T elem) {element = elem;}}
  • 以上代码是我和刘先润一起写的,所以相同。我们的代码和书上的代码也有一些不同。我们最初定义了两个节点。一个头结点一个尾节点。在构造函数将头节点指向为节点。在操作中头结点和尾节点都不存储数据。(但是后来发现到时没什么作用,到时代码比之书上并不简洁)

  • Android演示Stack

  • 我的思路用RecyclerView来完成此次app.
  • 1.首先要使用RecyclerView就要先添加依赖
    compile 'com.android.support:cardview-v7:21.0.3'compile 'com.android.support:recyclerview-v7:21.0.3'
  • 2.定义一个item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/title"android:layout_width="match_parent"android:layout_height="130dp"android:gravity="center"android:layout_margin="4dp"android:textColor="#99000000"android:textStyle="bold"android:textSize="22sp"android:background="@color/md_blue_100"android:text="0"/>
</LinearLayout>

1065456-20171015210845730-1954262155.jpg

  • 3.Activity布局
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="wb.is.besti.edu.cd.mystack.MainActivity">
<LinearLayoutandroid:id="@+id/liner"android:layout_width="match_parent"android:layout_height="60dp"android:layout_marginLeft="16dp"android:layout_marginRight="16dp"android:layout_marginBottom="16dp"><EditTextandroid:id="@+id/mtext"android:layout_width="match_parent"android:layout_height="match_parent" />
</LinearLayout><android.support.v7.widget.RecyclerViewandroid:id="@+id/list"android:clipToPadding="false"android:scrollbarStyle="outsideOverlay"android:layout_below="@+id/liner"android:layout_height="match_parent"android:layout_width="wrap_content"></android.support.v7.widget.RecyclerView>
</RelativeLayout>

1065456-20171015210920449-2064986727.jpg

  • 4.menu的布局
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_push"android:title="PUSH"android:orderInCategory="100"app:showAsAction="always"></item><itemandroid:id="@+id/action_pop"android:title="POP"android:orderInCategory="100"app:showAsAction="always"></item>
</menu>

1065456-20171015210932418-2013617227.jpg

  • 5.Activity
public class MainActivity extends AppCompatActivity {RecyclerView mRecyclerView;SimpleAdapter mAdapter;EditText mText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mText = (EditText)findViewById(R.id.mtext);mRecyclerView = (RecyclerView)findViewById(R.id.list);mRecyclerView.setLayoutManager(new LinearLayoutManager(this));mRecyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));mAdapter = new SimpleAdapter(this);mRecyclerView.setItemAnimator(new DefaultItemAnimator());mRecyclerView.setAdapter(mAdapter);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.popandpush,menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {int id = item.getItemId();if (id==R.id.action_push){mAdapter.push(mText.getText().toString());return true;}if (id==R.id.action_pop){mAdapter.remove(SimpleAdapter.LAST_POSITION);return true;}return super.onOptionsItemSelected(item);}
}
  • 6.适配器Adapter
public class SimpleAdapter extends RecyclerView.Adapter<SimpleAdapter.SimpleViewHolder> {public static  int LAST_POSITION = -1;private final Context mContext;private MyStack<String> myStack;public void push(String s){myStack.push(s);LAST_POSITION++;}public void remove(int positon){if (myStack.size()==0){}if (myStack.size()!=0){myStack.pop();notifyItemRemoved(positon);}}public static class SimpleViewHolder extends RecyclerView.ViewHolder{public final TextView title;public SimpleViewHolder(View view){super(view);title = (TextView)view.findViewById(R.id.title);}}public SimpleAdapter(Context context){mContext = context;myStack = new MyStack<>();}@Overridepublic SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {final View view = LayoutInflater.from(mContext).inflate(R.layout.simple_item,parent,false);return new SimpleViewHolder(view);}@Overridepublic int getItemCount() {return myStack.size();}@Overridepublic void onBindViewHolder(SimpleViewHolder holder, int position) {holder.title.setText(myStack.peek());}
}
  • 7.(非必须)添加分割线

  • 效果图
    1065456-20171015210951012-1920508582.png

  • RecyclerView是我在完成假期的工作记录APP时了解到的控件,RecyclerView只管回收与复用View,其他的你可以自己去设置。其高度的解耦,给予你充分的定制自由。进一步的加深使用时添加卫星菜单来实现isEmpty(),peek(),size()等操作及将界面进一步美化。

转载于:https://www.cnblogs.com/wbiao21/p/7673688.html

这篇关于20162321王彪 2017-2018《程序设计与数据结构》第五周学习总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/674623

相关文章

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

java常见报错及解决方案总结

《java常见报错及解决方案总结》:本文主要介绍Java编程中常见错误类型及示例,包括语法错误、空指针异常、数组下标越界、类型转换异常、文件未找到异常、除以零异常、非法线程操作异常、方法未定义异常... 目录1. 语法错误 (Syntax Errors)示例 1:解决方案:2. 空指针异常 (NullPoi

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Python依赖库的几种离线安装方法总结

《Python依赖库的几种离线安装方法总结》:本文主要介绍如何在Python中使用pip工具进行依赖库的安装和管理,包括如何导出和导入依赖包列表、如何下载和安装单个或多个库包及其依赖,以及如何指定... 目录前言一、如何copy一个python环境二、如何下载一个包及其依赖并安装三、如何导出requirem

Rust格式化输出方式总结

《Rust格式化输出方式总结》Rust提供了强大的格式化输出功能,通过std::fmt模块和相关的宏来实现,主要的输出宏包括println!和format!,它们支持多种格式化占位符,如{}、{:?}... 目录Rust格式化输出方式基本的格式化输出格式化占位符Format 特性总结Rust格式化输出方式

Java深度学习库DJL实现Python的NumPy方式

《Java深度学习库DJL实现Python的NumPy方式》本文介绍了DJL库的背景和基本功能,包括NDArray的创建、数学运算、数据获取和设置等,同时,还展示了如何使用NDArray进行数据预处理... 目录1 NDArray 的背景介绍1.1 架构2 JavaDJL使用2.1 安装DJL2.2 基本操

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

Git提交代码详细流程及问题总结

《Git提交代码详细流程及问题总结》:本文主要介绍Git的三大分区,分别是工作区、暂存区和版本库,并详细描述了提交、推送、拉取代码和合并分支的流程,文中通过代码介绍的非常详解,需要的朋友可以参考下... 目录1.git 三大分区2.Git提交、推送、拉取代码、合并分支详细流程3.问题总结4.git push