小编LMK*_*LMK的帖子

以不同顺序添加相同的双精度时的结果不同

为什么添加相同数字时输出会有所不同?

public class Test {

    public static void main(String a[]) {

        double[] x = new double[]{3.9, 4.3, 3.6, 1.3, 2.6};
        System.out.println(">>>>>>> " + sum(x));
    }

    public static double sum(double[] d) {

        double sum = 0;
        for (int i = 0; i < d.length; i++) {
            sum += d[i];
        }
        return sum;
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是: 15.7

如果我交换价值观

double[] x = new double[] {2.6, 3.9, 4.3, 3.6, 1.3};
Run Code Online (Sandbox Code Playgroud)

我得到输出为: 15.700000000000001

如何获得相同的输出?

java floating-point floating-accuracy

15
推荐指数
1
解决办法
1870
查看次数

什么是Android及其使用的严格性

我是android的新手,我正在按照本教程,我找到了下面的代码,在那里他将json字符串转换为StringEntity.纠正我,如果我错了StringEntity用于传递数据,接头像Accept,Content-type到Server.

            // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);



        String json = "";

        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("name", person.getName());
        jsonObject.accumulate("country", person.getCountry());
        jsonObject.accumulate("twitter", person.getTwitter());

        // 4. convert JSONObject to JSON to String
        json = jsonObject.toString();

        // ** Alternative way to convert Person object to JSON string usin Jackson Lib
        // ObjectMapper mapper = new ObjectMapper();
        // json = mapper.writeValueAsString(person);

        // 5. …
Run Code Online (Sandbox Code Playgroud)

java android json servlets

11
推荐指数
1
解决办法
2万
查看次数

如何在Jaxb编写的XML中添加<![CDATA [和]]>

如何使用CDATA准备XML,

我通过Jaxb预先做出这个回应,

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
 <SOAP-ENV:Header/>
 <soapenv:Body>
   <tem:RequestData>
     <tem:requestDocument>
        <![CDATA[
        <Request>
           <Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/>
           <Establishment Id="4297867"/>
        </Request>
        ]]>
      </tem:requestDocument>
   </tem:RequestData>
 </soapenv:Body>
 </soapenv:Envelope>  
Run Code Online (Sandbox Code Playgroud)

但是从Jaxb我没有得到CDATA,如何把CDATA放在<tem:requestDocument>元素中.

这是我的Java代码:

  public static String test1() {
    try {
        initJB();
        String response = null;
        StringBuffer xmlStr = null;
        String strTimeStamp = null;
        com.cultagent4.travel_republic.gm.Envelope envelope = null;
        com.cultagent4.travel_republic.gm.Header header = null;
        com.cultagent4.travel_republic.gm.Body body = null;
        com.cultagent4.travel_republic.gm.RequestData requestData = null;
        com.cultagent4.travel_republic.gm.RequestDocument requestDocument = null;
        com.cultagent4.travel_republic.gm.RequestDocument.Request request = null;
        com.cultagent4.travel_republic.gm.RequestDocument.Request.Authentication authentication = null;
        com.cultagent4.travel_republic.gm.RequestDocument.Request.Establishment establishment …
Run Code Online (Sandbox Code Playgroud)

java xml jaxb cdata

11
推荐指数
3
解决办法
2万
查看次数

Hibernate中的事务管理与DAO设计模式

我有很多表,对于每个表,我们都有DAO接口和DAOImplementation类.

示例DAO接口

public interface CancelPolicyDAO {

public CancelPolicy insertCancelPolicy(CancelPolicy cpdao)throws ChannelDispatcherException;

public CancelPolicy updateCancelPolicy(CancelPolicy cpdao)throws ChannelDispatcherException;

public void deleteCancelPolicy(CancelPolicy cpdao)throws ChannelDispatcherException;

public CancelPolicy findByCancelPolicyData(Integer id, Integer offSetUM, Integer nights, Float pOrAm, Byte isPercent)throws ChannelDispatcherException;

public CancelPolicy findByCancelPolicyId(Integer id)throws ChannelDispatcherException;
}
Run Code Online (Sandbox Code Playgroud)

示例DAOImplementation类

public class CancelPolicyDAOImpl implements CancelPolicyDAO {

@Override
public CancelPolicy insertCancelPolicy(CancelPolicy bean) throws ChannelDispatcherException {

    Session ses = null;
    try {

        ses = HibernateConnector.getInstance().getSession();
        ses.save(bean);
        ses.flush();
        return bean;
    } catch (Exception e) {
        e.printStackTrace();
        throw new ChannelDispatcherException(DbUtil.getStackTraceMessage(e));
    } finally { …
Run Code Online (Sandbox Code Playgroud)

java dao design-patterns hibernate transactions

7
推荐指数
2
解决办法
8648
查看次数

CyclicBarrier 重用示例

在这种情况下,CyclicBarrier 是否最合适。我想在 Stages 中并行运行 n 个线程(在 Stages 等待直到所有线程完成该 Stage)。

        public class CyclicBarr {


        public static void main(String[] args) {


            CyclicBarrier barrier = new CyclicBarrier(3, new Runnable() {
                private int count =1;
                @Override
                public void run() {
                    System.out.println("Completed..!! "+(count++));
                }
            });

            for (int i = 1; i <= 3; i++) {

                Thread t = new Thread(new CuclicBarThread(barrier));
                t.start();

            }



        }
    }
Run Code Online (Sandbox Code Playgroud)

线程是

        public class CuclicBarThread implements Runnable {

        CyclicBarrier barrier;

        public CuclicBarThread(CyclicBarrier barrier) {
            this.barrier = barrier;
        }

        @Override
        public void …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading

5
推荐指数
1
解决办法
1548
查看次数

如何使用CDATA创建SOAP请求

我是SOAP新手,我想创建SOAP请求,如下所示

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
 <SOAP-ENV:Header/>
<soapenv:Body>
  <tem:RequestData>
     <tem:requestDocument>
        <![CDATA[
        <Request>
           <Authentication CMId="68" Function="1" Guid="5594FB83-F4D4-431F-B3C5-EA6D7A8BA795" Password="poihg321TR"/>
           <Establishment Id="4297867"/>
        </Request>
        ]]>

     </tem:requestDocument>
  </tem:RequestData>
</soapenv:Body>
</soapenv:Envelope>  
Run Code Online (Sandbox Code Playgroud)

我在教程中找到了创建SOAP请求的代码

  MessageFactory mf = MessageFactory.newInstance();
                    SOAPMessage sm = mf.createMessage();
                    SOAPEnvelope envelope = sm.getSOAPPart().getEnvelope();
                    envelope.addNamespaceDeclaration("soap", "http://schemas.xmlsoap.org/soap/envelope/");
                    envelope.setPrefix("soapenv");

                    envelope.setAttribute("xmlns:tem", "http://tempuri.org/");       

                    SOAPBody body = envelope.getBody();
                    body.setPrefix("soapenv");

                    SOAPElement requestData = body.addChildElement("RequestData");
                    requestData.setPrefix("tem");

                    SOAPElement requestDoc = requestData.addChildElement("requestDocument","tem","http://tempuri.org/");

                    requestDoc.addTextNode(" <![CDATA[");

                    SOAPElement request = requestDoc.addChildElement("Request");

                    SOAPElement authentication = request.addChildElement("Authentication");
                    authentication.setAttribute("CMId", "68");
                    authentication.setAttribute("Guid", "5594FB83-F4D4-431F-B3C5-EA6D7A8BA795");
                    authentication.setAttribute("Password", "poihg321TR");
                    authentication.setAttribute("Function", "1");

                     SOAPElement establishment = request.addChildElement("Establishment");
                        establishment.setAttribute("Id", …
Run Code Online (Sandbox Code Playgroud)

java soap web-services cdata

4
推荐指数
1
解决办法
2万
查看次数

获取对匿名内部类的类对象的引用

如何在Java中获得对匿名内部类的类对象的引用?

对于非无限级的类,它完成了ClassName.class.

java anonymous-class

4
推荐指数
1
解决办法
258
查看次数

为什么我们将对象存储在接口引用中?

我对接口有疑问.
我看过一些代码,他们正在写作

Connection con = Drivermanager.getConnection("URL","User name","Pwd");
Run Code Online (Sandbox Code Playgroud)

这里con(连接接口的参考变量)持有Oracle连接对象.因为我们知道我们正在创建Oracle连接对象,为什么我们不能写

OracleConnectionClass Oraclecon = Drivermanager.getConnection("URL","User name","Pwd");
Run Code Online (Sandbox Code Playgroud)

如果我问一些人他们说,他们通过使用连接接口实现多态性.

但是,有
什么用呢.
我们可以做的

两个引用(con和Oraclecon)创建语句,执行查询等.

simillarly for List和ArrayList
提前感谢

java polymorphism jdbc

2
推荐指数
1
解决办法
1165
查看次数