例如:if:
String str = "100101010";
然后应该发生以下事情:
a[0]=1
a[1]=0
a[2]=0
...
它也应该适用于大字符串(最多64个字符)。
答案 0 :(得分:1)
一个提示:找到ASCII的图表。 http://www.asciitable.com/index/asciifull.gif" 0"的代码是48和" 1"的代码是49.您可以通过从ASCII值中减去48来将字符转换为数字。
int value = str.charAt(0) - 48;
认识到Java char
只是一个整数类型,就像int
和byte
一样重要。你可以用它们做数学。考虑到这个想法,你应该能够自己弄清楚其余部分。
(这在技术上是重复的,因为很久以前我回答了类似的问题,但我找不到它,所以你得到一个免费赠品。)
答案 1 :(得分:1)
import { Component, Inject, Injectable } from '@angular/core';
import { DOCUMENT } from '@angular/platform-browser';
import { Title } from '@angular/platform-browser';
@Injectable()
export class SeoService {
/**
* Angular 2 Title Service
*/
/**
* <head> Element of the HTML document
*/
private headElement: HTMLElement;
/**
* <meta name="description"> Element of the document head
*/
private metaDescription: HTMLElement;
/**
* <meta name="robots"> Element of the document head
*/
private robots: HTMLElement;
dom:any;
/**
* Inject the Angular 2 Title Service
* @param titleService
*/
constructor (@Inject(DOCUMENT) private document: any, private titleService: Title ){
this.titleService = titleService;
/**
* get the <head> Element
* @type {any}
*/
this.dom = document;
this.headElement = this.dom.getSelection('head');
this.metaDescription = this.getOrCreateMetaElement('description');
this.robots = this.getOrCreateMetaElement('robots');
}
public getTitle(): string {
return this.titleService.getTitle();
}
public setTitle(newTitle: string) {
this.titleService.setTitle(newTitle + ' | Stareable');
}
public getMetaDescription(): string {
return this.metaDescription.getAttribute('content');
}
public setMetaDescription(description: string) {
this.metaDescription.setAttribute('content', description);
}
public getMetaRobots(): string {
return this.robots.getAttribute('content');
}
public setMetaRobots(robots: string) {
this.robots.setAttribute('content', robots);
}
/**
* get the HTML Element when it is in the markup, or create it.
* @param name
* @returns {HTMLElement}
*/
private getOrCreateMetaElement(name: string): HTMLElement {
let el: HTMLElement;
el = this.dom.getSelection('meta[name=' + name + ']');
if (el === null) {
el = this.dom.getAttribute('meta');
el.setAttribute('name', name);
this.headElement.appendChild(el);
}
return el;
}
}
如果它可以使用shor或byte类型,因为它可以存储0,1并且你消耗的内存少于int
答案 2 :(得分:-2)
我不知道你为什么要这样做,但这是一个简单的问题。
public static void main (String[] args) {
String s = "111232";
String element[] = s.split("");
System.out.println(element[0]);
System.out.println(element[1]);
System.out.println(element[2]);
System.out.println(element[3]);
System.out.println(element[4]);
System.out.println(element[5]);
}