arraylist

Comparing array list of string array

若如初见. 提交于 2021-02-19 07:42:08
问题 I want to compare two ArrayList of string arrays. List<String[]> list1 = new ArrayList<String[]>; List<String[]> list2 = new ArrayList<String[]>; list1.equals(list2); This will return false because equals method in ArrayList will do equals on the element. ListIterator<E> e1 = listIterator(); ListIterator<?> e2 = ((List<?>) o).listIterator(); while (e1.hasNext() && e2.hasNext()) { E o1 = e1.next(); Object o2 = e2.next(); if (!(o1==null ? o2==null : o1.equals(o2))) return false; } return !(e1

How to write multiple ArrayList in a text file

天大地大妈咪最大 提交于 2021-02-17 07:19:05
问题 In below code , i'm trying to create a txt file and there i want to add some list of values. So here my approach is create a new file if file is not present and add the respective elements into it. Please see below , i'm unable to get my expected output, So can you help me with some idea so it will be very helpful public class MyTest { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub SftpConn sftp = new SftpConn(); //sftp.sftpGetConnect(); List

Removing from Java ArrayList without iteration

爱⌒轻易说出口 提交于 2021-02-17 07:06:10
问题 I am working on an assignment, and I can't find a way to make this work properly. I have an ArrayList with information on 5 students. I need to construct a method that will remove one of the objects, but I do not need/want iteration, as it is a static removal. I have the following code: import java.util.ArrayList; import java.lang.String; public class Roster { static ArrayList<Student> myRoster = new ArrayList<>(); public static void main(String[] args){ //Add Student Data Roster.add("1",

Java increase array by X size

浪子不回头ぞ 提交于 2021-02-17 05:18:20
问题 I need an array that I can determine its size from the start and if it has no more empty spaces then increase its size by X . For example: int arraySize = 2; int[] intArray = new int[arraySize]; int counter = 0; while(something){ if(counter == arraySize){ // increase array size by X } intArray[counter] = counter; counter++; } I checked ArrayList and Vector but I did not find a way. 回答1: I think ArrayList is better in this case instead of simple arrays because automatically grows. From the

Variable cannot be resolved in an If-Statement

大城市里の小女人 提交于 2021-02-17 03:22:16
问题 I'm trying to do the below tutorial question. // Create a method called greatestCommonFactor // It should return the greatest common factor // between two numbers. // // Examples of greatestCommonFactor: // greatestCommonFactor(6, 4) // returns 2 // greatestCommonFactor(7, 9) // returns 1 // greatestCommonFactor(20, 30) // returns 10 // // Hint: start a counter from 1 and try to divide both // numbers by the counter. If the remainder of both divisions // is 0, then the counter is a common

How I can access array elements of List

旧城冷巷雨未停 提交于 2021-02-16 22:46:51
问题 I have a question: I have a List Java that I have populated with different values. For example, I have: List<String[]> l = new ArrayList(); String[] lis = new String[3]; lis[0] = "A"; lis[1] = "B"; lis[2] = "C"; l.add(lis); and I have other values too. Now, I want to get the search in this list only the first field. For example, I want the A's indexOf. I have tried to write this code: int index = l.indexOf("A"); but I get -1 as return. I would like to know how I can access to a field of the

How I can access array elements of List

懵懂的女人 提交于 2021-02-16 22:45:30
问题 I have a question: I have a List Java that I have populated with different values. For example, I have: List<String[]> l = new ArrayList(); String[] lis = new String[3]; lis[0] = "A"; lis[1] = "B"; lis[2] = "C"; l.add(lis); and I have other values too. Now, I want to get the search in this list only the first field. For example, I want the A's indexOf. I have tried to write this code: int index = l.indexOf("A"); but I get -1 as return. I would like to know how I can access to a field of the

How I can access array elements of List

浪尽此生 提交于 2021-02-16 22:45:20
问题 I have a question: I have a List Java that I have populated with different values. For example, I have: List<String[]> l = new ArrayList(); String[] lis = new String[3]; lis[0] = "A"; lis[1] = "B"; lis[2] = "C"; l.add(lis); and I have other values too. Now, I want to get the search in this list only the first field. For example, I want the A's indexOf. I have tried to write this code: int index = l.indexOf("A"); but I get -1 as return. I would like to know how I can access to a field of the

Extract integers from string and add them in Java [duplicate]

我与影子孤独终老i 提交于 2021-02-16 15:25:06
问题 This question already has answers here : how to extract numeric values from input string in java (16 answers) Closed 4 years ago . I want to extract the integers from string and add them. Ex : String s="ab34yuj789km2"; I should get the output of integers from it as 825 ( i.e., 34 + 789 + 2 = 825 ) 回答1: Here's one way, by using String.split: public static void main(String[] args) { String s="ab34yuj789km2"; int total = 0; for(String numString : s.split("[^0-9]+")) { if(!numString.isEmpty()) {

【Java基础】集合

萝らか妹 提交于 2021-02-15 12:30:02
一、Collection接口(超级接口:Iterator) 其下有两个子接口,Set和List。 1)Set接口的两个常用子类 TreeSet:有序存放 HashSet:散列存放 2)List接口(允许重复元素) 常用子类:LinkedList、ArrayList、Vector。ArrayList是List接口最常用的一个子类。LinkedList完成的是一个链表操作。 二、Map接口 实现子类:HashMap、HashTable、TreeMap等 TreeMap与TreeSet类似,TreeMap中元素根据key自动排序。 三、集合元素为自定义类时,集合的排序问题及 输出 自定义类作为集合元素时,集合本身不知道如何排序,必须类实现Comparable接口,并覆写compareTo()方法。 public class Person implements Comparable<Person> { private String name; private int age; public Person(String name, int age) { this.age = age; this.name = name; } @Override public int compareTo(Person o) {//按名字顺序排序 return this.name.compareTo(o.name