我有这个链表:
LinkedList<Cookies> linkList = new LinkedList<>();
linkList.add(new Cookies("Name1", 2, 2));
linkList.add(new Cookies("Name2", 3, 1));
linkList.add(new Cookies("Name3", 1, 6));
linkList.add(new Cookies("Name4", 2, 2));
linkList.add(new Cookies("Name2", 4, 2));
我将如何搜索&#34; Name2&#34;并输出:
Name2, 3, 1
Name2, 4, 2
我已经这样做但是它返回false / not found
boolean found = linkList.contains(new Cookies("Name2", 3, 1));
System.out.println("Found: " + found);
答案 0 :(得分:2)
您需要在from subprocess import check_output, call
import pandas as pd
import numpy as np
import os
pad = 0.1
tablename = 'sandbox.max.pybcp_test'
overwrite=True
raise_exception = True
server = 'P01'
trusted_connection= True
username=None
password=None
delimiter='|'
df = pd.read_csv('D:/inputdata.csv', encoding='latin', error_bad_lines=False)
def get_column_def_sql(col):
if col.dtype == object:
width = col.str.len().max() * (1+pad)
return '[{}] varchar({})'.format(col.name, int(width))
elif np.issubdtype(col.dtype, float):
return'[{}] float'.format(col.name)
elif np.issubdtype(col.dtype, int):
return '[{}] int'.format(col.name)
else:
if raise_exception:
raise NotImplementedError('data type {} not implemented'.format(col.dtype))
else:
print('Warning: cast column {} as varchar; data type {} not implemented'.format(col, col.dtype))
width = col.str.len().max() * (1+pad)
return '[{}] varchar({})'.format(col.name, int(width))
def create_table(df, tablename, server, trusted_connection, username, password, pad):
if trusted_connection:
login_string = '-E'
else:
login_string = '-U {} -P {}'.format(username, password)
col_defs = []
for col in df:
col_defs += [get_column_def_sql(df[col])]
query_string = 'CREATE TABLE {}\n({})\nGO\nQUIT'.format(tablename, ',\n'.join(col_defs))
if overwrite == True:
query_string = "IF OBJECT_ID('{}', 'U') IS NOT NULL DROP TABLE {};".format(tablename, tablename) + query_string
query_file = 'c:\\pybcp_tempqueryfile.sql'
with open (query_file,'w') as f:
f.write(query_string)
if trusted_connection:
login_string = '-E'
else:
login_string = '-U {} -P {}'.format(username, password)
o = call('sqlcmd -S {} {} -i {}'.format(server, login_string, query_file), shell=True)
if o != 0:
raise BaseException("Failed to create table")
# o = call('del {}'.format(query_file), shell=True)
def call_bcp(df, tablename):
if trusted_connection:
login_string = '-T'
else:
login_string = '-U {} -P {}'.format(username, password)
temp_file = 'c:\\pybcp_tempqueryfile.csv'
#remove the delimiter and change the encoding of the data frame to latin so sql server can read it
df.loc[:,df.dtypes == object] = df.loc[:,df.dtypes == object].apply(lambda col: col.str.replace(delimiter,'').str.encode('latin'))
df.to_csv(temp_file, index = False, sep = '|', errors='ignore')
o = call('bcp sandbox.max.pybcp_test2 in c:\pybcp_tempqueryfile.csv -S "localhost" -T -t^| -r\n -c')
课程中实施equals
方法,以便Cookies
按预期工作
linkList.contains
否则将调用class Cookie{
@Override
boolean equals(Object cookie){
..
}
}
来检查引用相等性,这意味着
Object.equals
总是假的
答案 1 :(得分:0)
如果两个Cookie对象包含相同的值(或者至少是相同的名称),则必须在Cookie类中实现equals()方法,以返回true。
另外,如果equals()返回true,则实现hashCode()方法为Cookie对象返回相同的值。
答案 2 :(得分:0)
如果这是你开始学习Java的话,那么我想这就是了解列表如何工作以及如何循环列表并覆盖toString等。
示例如下所示。
import java.util.*;
public class TTT {
public static void main(String[] argv) {
LinkedList<Cookies> linkList = new LinkedList<>();
linkList.add(new Cookies("Name1", 2, 2));
linkList.add(new Cookies("Name2", 3, 1));
linkList.add(new Cookies("Name3", 1, 6));
linkList.add(new Cookies("Name4", 2, 2));
linkList.add(new Cookies("Name2", 4, 2));
for(int i=0; i<linkList.size(); i++ ) {
Cookies c = linkList.get(i);
if( c.getName().equals("Name2")) {
System.out.println(c);
}
}
}
}
class Cookies {
String n;
int a;
int b;
public Cookies(String n, int a, int b) {
this.n = n;
this.a = a;
this.b = b;
}
public String getName() {
return n;
}
public String toString() {
return n+", " + a + ", " + b;
}
}