网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java生成泡泡糖代码 java消灭泡泡糖

50分!请问个java基础问题

这个是java中的不定长参数(Variable-length Argument),下面详细说明一下:

10年积累的网站设计制作、做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有乐业免费网站建设让你可以放心的选择与我们合作。

不定长参数的由来:

我们在调用某个方法的时候,如果事先无法决定参数的个数,例如最简单的输出语句:System.out.println();这里面是无法事先决定要给的参数的个数的,比如输出一个a1,就要写System.out.println(a1);如果输出两个,就要改为System.out.println(a1,a2);怎么解决这个问题呢?

自J2se5.0之后开始支持不定长参数(Variable-length Argument),这样就可以直接解决上述问题,来看一个范例:

public class Variable{

public static int sum(int... nums){ //使用...声明参数

int sum = 0;

for(int num : nums){

sum+=num;

}

return sum;

}

这个范例就介绍了不定长参数的用法,其中...就是它的标志,要加在类型关键词后面。其实仔细分析一下可知,nums就是一个数组,编译器会将参数行的(int... nums)解释为(int[] nums)。下面我们就来使用一下上面的这个使用了不定长参数的方法,代码如下:

public class TestVariable{

public static void main(String[] args){

int sum = 0;

sum = Variable.sum(1,2);

System.out.println("1+2="+sum);

sum = Variable.sum(1,2,3);

System.out.println("1+2+3="+sum);

sum = Variable.sum(1,2,3,4,5);

System.out.println("1+2="+sum);

}

}

}

分析可知,编译器会将传递给方法的参数解释为int数组传入至sum()中,所以实际上不定长参数的功能也是自J2se5.0之后所提供的编译蜜糖(Compiler Sugar).

现在来说一下你说的情况,你说的public void method(Class... aaa){} ,这个是使用对象的不定长参数,这个Class当然可以换成其他的东西了,可以换成任何能够作为参数来传递的东西,相信看了上面的话,你也早就明白了,呵呵……

最后我还要说明一下不定长参数使用时候的注意事项:

1:在使用不定长参数时,记得必须声明的参数必须设置在参数行的最后一个,来具体看几个例子:

public void someMethod(int arg1,int arg2,int... varargs){} (正确的)

public void someMethod(int... varargs,int arg1,int arg2){} (错误的)

2:不能使用两个以上的不定长参数,例如下面的方式是不合法的

public void someMethod(int... varargs1,int... varargs2){} (不合法)

3:如果使用对象的不定长参数,声明的方法相同,例如:

public void someMethod(someClass... somes){}

以上就是我所知道的不定长参数的相关知识,在这里献丑了……

如果有高人了解的更多,我愿闻其详……

最后,祝所有和我一样在自学java的兄弟们,早日出头……

{system.out.println("");}'>java return () -> {system.out.println("");}

这是lambda表达式, 一种语法(糖) ,可以简化代码,从java8开始支持lambda表达式.

有的编程语言,很多早就有lambda表达式了,  java从8才开始支持lambda表达式,算比较晚的了.

我们以实现Runabble接口,来创建一个线程为例

一: 使用传统的匿名内部类来实现,

Thread t1 = new Thread(new Runnable() {

@Override

public void run() {

System.out.println("匿名内部类的方式实现Runnable接口");

}

});

t1.start();

很多人觉得上面的写法比较啰嗦, 写那么多代码, 核心就是一句System.out.println("....")

二: lambda表达式实现

Thread t2 = new Thread(() - {System.out.println("lambda表达式的实现Runabble接口");});

t2.start();

lambda表示配合Java8流, 进行函数式编程, 可以简洁的完成很多比较啰嗦的逻辑 .

比如对一个ListDouble进行求和,求均值,求最大值,求最小值,元素个数. 以前需要写较多的代码.

Java8流和lambda表达式的操作方法如下

// lambda表达式配合java8流

ListDouble list = Arrays.asList(5.9, 4.5, 6.2, 1.8, 3.7, 2.9, 2.52);

DoubleSummaryStatistics ds = list.stream().collect(Collectors.summarizingDouble(x - x));

System.out.println("最小值:" + ds.getMin());

System.out.println("最大值:" + ds.getMax());

System.out.println("平均值:" + ds.getAverage());

// System.out.println("数量:" + ds.getCount());

// System.out.println("总和:" + ds.getSum());

相关的知识, 还是比较多, 建议先学习lambda表达式,然后学习java8流(stream)

求java大佬帮忙

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class Recipe {

private String name; //菜谱名称

private String style; //菜系 如:川菜、湘菜等

private int time; //烹饪时长 分钟

private String[] food; //食材

private String[] step; //操作步骤

public Recipe() {

}

public Recipe(String name, String style, int time, String[] food, String[] step) {

this.name = name;

this.style = style;

this.time = time;

this.food = food;

this.step = step;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getStyle() {

return style;

}

public void setStyle(String style) {

this.style = style;

}

public int getTime() {

return time;

}

public void setTime(int time) {

this.time = time;

}

public String[] getFood() {

return food;

}

public void setFood(String[] food) {

this.food = food;

}

public String[] getStep() {

return step;

}

public void setStep(String[] step) {

this.step = step;

}

@Override

public String toString() {

String foods="";

for (String f : food) {

foods+=f+" ";

}

String steps="";

for (int i = 0; i step.length; i++) {

steps += (i+1)+"."+step[i];

if(i!=step.length-1){

steps+=";";

}

}

return "菜谱名称:" + name +

"\n菜系:" + style +

"\n时长:" + time +

"\n所需食材:" + foods +

"\n操作步骤:" + steps;

}

public static void print(Recipe[] recipes){

for (Recipe recipe : recipes) {

System.out.println(recipe);

}

}

public static Recipe[] searchRecipesContainName(Recipe[] recipes, String name){

ListRecipe list=new ArrayListRecipe();

for (Recipe recipe : recipes) {

if(recipe.getName().contains(name)){

list.add(recipe);

}

}

return list.toArray(new Recipe[list.size()]);

}

public static Recipe[] searchRecipes(Recipe[] recipes, String style){

ListRecipe list=new ArrayListRecipe();

for (Recipe recipe : recipes) {

if(recipe.getStyle().equals(style)){

list.add(recipe);

}

}

return list.toArray(new Recipe[list.size()]);

}

public static Recipe[] searchRecipeLessThan(Recipe[] recipes, int time){

ListRecipe list=new ArrayListRecipe();

for (Recipe recipe : recipes) {

if(recipe.getTime()time){

list.add(recipe);

}

}

return list.toArray(new Recipe[list.size()]);

}

public static Recipe[] searchRecipeContainsFood(Recipe[] recipes, String food){

ListRecipe list=new ArrayListRecipe();

for (Recipe recipe : recipes) {

for (String s : recipe.getFood()) {

if(s.equals(food)){

list.add(recipe);

}

}

}

return list.toArray(new Recipe[list.size()]);

}

public static void main(String[] args) {

//存储5个菜谱的信息

Recipe[] recipes=new Recipe[5];

recipes[0]=new Recipe("酱牛肉","家常菜",120,

new String[]{

"牛腱子",

"黄豆酱油",

"黄酒",

"冰糖"

},

new String[]{

"准备好主要食材",

"加入食材慢炖两至三小时"

});

recipes[1]=new Recipe("红烧牛肉","家常菜",120,

new String[]{

"牛腩",

"牛筋",

"生抽",

"冰糖"

},new String[]{

"准备好主要食材",

"加入食材慢炖两至三小时"

});

recipes[2]=new Recipe("小鸡炖蘑菇","湘菜",100,

new String[]{

"小鸡",

"蘑菇"

},

new String[]{

"准备好主要食材",

"加入食材慢炖两至三小时"

});

recipes[3]=new Recipe("地三鲜","川菜",25,

new String[]{

"茄子",

"辣椒"

},

new String[]{

"准备好主要食材",

"加入食材慢炖"

});

recipes[4]=new Recipe("西红柿","湘菜",20,

new String[]{

"西红柿",

"味达美酱油"

},

new String[]{

"准备好主要食材",

"加入食材慢炖"

});

System.out.println("找到所有名称中包含牛肉的菜谱:");

Recipe[] result_1 = searchRecipesContainName(recipes, "牛肉");

print(result_1);

System.out.println("\n希望查找所有湘菜的菜谱:");

Recipe[] result_2=searchRecipes(recipes,"湘菜");

print(result_2);

System.out.println("\n查找烹饪时长小于30分钟的菜谱:");

Recipe[] result_3=searchRecipeLessThan(recipes,30);

print(result_3);

System.out.println("\n查找包含西红柿的菜谱:");

Recipe[] result_4=searchRecipeContainsFood(recipes,"西红柿");

print(result_4);

}

}

Java简单算法问题

初步做了一个出来,但是效率并不是很高,前100个计算速度还可以,但是往后就很慢了。如果什么时候有空的话可以再看看,先给你代码吧,不知道能不能帮上你

public class AlisandaNumber {

private static final int MAX_INDEX = 1000; // 可以先把这个常量改为1-6,验证正确性

public static void main(String[] args) {

int a = 0;

int index = 0;

while(index  MAX_INDEX) {

a += 6; // 每次循环自增6,由题目规律可知A是6的倍数

boolean breakOut = false;

// 最大的约数为此数的平方根,因为如果是两个平方根相乘的话,剩下的就只有1了

int maxNum = (int) Math.ceil(Math.sqrt(a));

p:

for(int p = 1; p = maxNum; p ++) {

if(a % p != 0) {

continue; // 如果不是约数的话,没必要考虑,下同

}

// 最大约数为平方根的相反数,原理同上

maxNum = (int) Math.ceil(Math.sqrt(a / p));

for(int q = -1; q = -maxNum; q --) { // q和r必为负数

if(a % q != 0) {

continue;

}

int r = a / (p * q);

int nonZero = p * q + p * r + q * r;

if (nonZero == 0) {

continue;

}

if((a == p * q * r)  (a == (p * q * r) / (nonZero))) {

index ++;

breakOut = true;

break p; // 跳出外层循环

}

}

}

if(breakOut) {

System.out.println(String.format("第%d个压力山大数是%d", index, a));

}

}

}

}


本文题目:java生成泡泡糖代码 java消灭泡泡糖
网页地址:http://bjjierui.cn/article/ddegjdc.html

其他资讯