java数据结构训练

1、 定义一个方法 listTest(ArrayList<String> list, String name),要求返回
name 在 list 里面第一次出现的索引,如果 name 没出现过返回-1。  
package collection;

import java.util.ArrayList;
import java.util.Scanner;

public class demo1 {
	
	public static void main(String[] args) {
		 Scanner in = new Scanner(System.in);
	        ArrayList<String> nameList = new ArrayList<>();
	        nameList.add("小A");
	        nameList.add("小B");
	        nameList.add("呵呵君");
	        nameList.add("哒哒哒");
	        while (true){
	            System.out.println("你要找谁:");
	            String name = in.nextLine();
	            int index = listTest(nameList, name);
	            if (index == -1){
	                System.out.println("没这个人\n");
	            } else{
	                System.out.println(name + "的index是:" + index + "\n");
	            }
	        }
	    }

	    public static int listTest(ArrayList<String> list, String name){
	        int size = list.size();
	        for (int i = 0; i < size; i++) {
	            if (list.get(i).equals(name)){
	                return i;
	            }
	        }
	        return -1;
	}

}
2、已知数组存放一批 QQ 号码,长度 5-11 位,
String[] strs = {"10001","10086","12347806666","45612378901","10001","12347806666"}。
将该数组里面的所有 qq 号都存放在 LinkedList 中,将 list 中重复元素删
除,将 list 中所有元素分别用迭代器和增强 for 循环打印出来
package collection;

import java.util.Iterator;
import java.util.LinkedList;

public class demo2 {

	 public static void main(String[] args) {
	        String[] strs = {"10001","10086","12347806666","45612378901","10001","12347806666"};
	        LinkedList<String> qqList = getList(strs);

	        // 迭代器打印
	        System.out.println("------------迭代器------------");
	        Iterator<String> i = qqList.iterator();
	        while (i.hasNext()){
	            System.out.print(i.next() + " ");
	        }

	        System.out.println();

	        // 增强for循环打印
	        System.out.println("----------增强for循环---------");
	        for (String name: qqList) {
	            System.out.print(name +" ");
	        }


	    }

	    // 获取无重复的List
	    public static LinkedList<String> getList(String[] strs){
	        LinkedList<String> list = new LinkedList<>();
	        for (int i = 0; i < strs.length; i++) {
	            if (!list.contains(strs[i])){
	                list.add(strs[i]);
	            }
	        }
	        return list;
	    }
	}
3、分别用Comparable和Comparator两个接口对下列四位同学的成绩做降
序排序,如果成绩一样,那在成绩排序的基础上按照年龄由小到大排序。

 

package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class demo3 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		  Student s1 = new Student("贾宝玉",14,88.5);
	        Student s2 = new Student("林黛玉",13,90.5);
	        Student s3 = new Student("史湘云",13,85.5);
	        Student s4 = new Student("薛宝钗",15,91.0);
	        Student s5 = new Student("工具人",15,88.5);
	        ArrayList<Student> studentList = new ArrayList<>();
	        studentList.add(s1);
	        studentList.add(s2);
	        studentList.add(s3);
	        studentList.add(s4);
	        studentList.add(s5);
	        System.out.println("--------comparable实现方法--------");
	        System.out.println("原来的顺序");
	        for(Student s : studentList){
	            System.out.println(s);
	        }
	        System.out.println("--------------"+'\n' + "现在的顺序:");
	        Collections.sort(studentList);
	        for(Student s : studentList){
	            System.out.println(s);
	        }

	        System.out.println("--------comparator实现方法--------");
	        System.out.println("原来的顺序");
	        for(Student s : studentList){
	            System.out.println(s);
	        }
	        System.out.println("--------------"+'\n' + "现在的顺序:");
	        Collections.sort(studentList,new comparator());
	        for(Student s : studentList){
	            System.out.println(s);
	        }
	    }

	    static class Student implements Comparable<Student>{
	        private String name;
	        private int age;
	        private double points;

	        public Student(String name, int age, double points) {
	            this.name = name;
	            this.age = age;
	            this.points = points;
	        }

	        @Override
	        public String toString() {
	            return "Student{" +
	                    "name='" + name + '\'' +
	                    ", age=" + age +
	                    ", points=" + points +
	                    '}';
	        }

	        @Override
	        public int compareTo(Student o) {
	            if(o.points == this.points){
	                return this.age - o.age;
	            }
	            if(o.points - this.points > 0){
	                return 1;
	            }else if(o.points - this.points < 0){
	                return -1;
	            }else{
	              return 0;
	            }

	        }
	    }

	    static class comparator implements Comparator<Student> {

	        @Override
	        public int compare(Student o1, Student o2) {
	            if(o1.points == o2.points){
	                return o1.age - o2.age;
	            }

	            if(o1.points - o2.points > 0){
	                return -1;
	            }else if(o1.points - o2.points < 0){
	                return 1;
	            }else{
	                return 0;
	            }
	        }

	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java可以使用Neo4j来进行NLP(自然语言处理)的数据训练。Neo4j是一种图形数据库,它的图形结构非常适合处理NLP任务中的复杂关系和语义网络。 在Java中使用Neo4j,我们首先需要导入Neo4j的相关库和依赖。然后,我们可以使用Java的Neo4j驱动程序来连接和操作Neo4j数据。通过这个驱动程序,我们可以执行查询、插入、更新和删除数据等操作。 在NLP数据训练中,我们可以将文本数据转换为图形结构,并使用Neo4j来存储和处理这些数据。例如,我们可以将句子中的词和实体作为节点,将它们之间的关系(如依赖关系、语义关系)作为边。通过构建这样的图形结构,我们可以更好地表示和理解文本中的语义关系。 使用Neo4j进行NLP训练的一个常见任务是实体识别和关系抽取。我们可以使用已有的语料库和机器学习算法来训练模型,然后将模型结果存储到Neo4j中。这样,在实际应用中,我们可以使用已经训练好的模型来进行实体识别和关系抽取。 此外,通过Neo4j的图形查询语言Cypher,我们可以方便地进行复杂的查询和分析。例如,我们可以通过Cypher查询来查找具有特定关系的实体对,或者查找具有特定属性的实体。 总之,Java可以使用Neo4j来进行NLP数据训练。Neo4j的图形数据库特性使得它非常适合处理NLP中的复杂关系和语义网络。我们可以利用Neo4j的功能来存储、处理和查询NLP数据,从而提高NLP任务的效率与精度。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

H_IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值