Ⅰ java題目:創建一個一維數組,每個元素的值是100-999之間的隨機整數.輸出該數組的元素,求幫忙,急
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Random;
importjava.util.Date;
publicclassSolution{
int[]rlist;
Randomr;
Integermax=null;
intlength;
intcursor;
publicSolution(intlength){
this.length=length;
}
publicintgetLength(){
returnlength;
}
publicvoidsetLength(intlength){
this.length=length;
}
privateintgetRnum(){
returnr.nextInt(900)+100;
}
privatevoidcheckIfMax(){
max=cursor==0?0:(rlist[max]>=rlist[cursor]?max:cursor);
}
publicvoidrun(){
rlist=newint[length];
max=null;
r=newRandom((newDate()).hashCode());
for(cursor=0;cursor<length;cursor++){
rlist[cursor]=getRnum();
checkIfMax();
System.out.print(rlist[cursor]);
if(cursor==length-1){
System.out.print(" ");
}elseif((cursor+1)%5==0){
System.out.print(", ");
}else{
System.out.print(",");
}
}
System.out.println("MAX:"+rlist[max]+",Location:"+max);
}
publicstaticvoidmain(String[]args){
Solutions=newSolution(123);
s.run();
}
}
Ⅱ 創建一個整型一維數組md特擁有100個元素且各元素值為0.0到1.0之間的任一隨機數然後再求各個元素的平均值
MyJsp1.jsp:
<%@ page language="java" import="java.util.*,work.*" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<jsp:useBean id="user" class="work.User" scope="request"/>
<jsp:setProperty name="user" property="name" value="Hello World!"/>
<jsp:getProperty name="user" property="name"/>
<form action="MyJsp2.jsp">
<input type="submit" value="確定">
</form>
</body>
</html>
/**************************************************/
MyJsp2.jsp:
<%@ page language="java" import="java.util.*,work.*" pageEncoding="UTF-8"%>
<html>
<head>
</head>
<body>
<%
User username = (User)request.getAttribute("user");
%>
<%=username.getName() %>
</body>
</html>
/**************************************************/
User.jsp:
package work;
public class User {
String name;
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
}
/***********************/
Ⅲ C++動態創建一維數組,實現n*n矩陣轉置
有個問題,最後刪除動態數組應該是delete[] p;
然後VC++編譯器規定二維數組的維數必須是常量,你那個N是變數,GCC可以
Ⅳ C語言:創建一個一維(int)數組(維數為6),輸入數組元素,並輸出數組元素中值最大的一個。
#include <stdio.h>
int main()
{
int a[6],i,j,temp;
printf("輸入6個數:\n");
for(i=0;i<6;i++)
scanf("%d",&a[i]);
for(i=0;i<6;i++)
for(j=i+1;j<6;j++)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("最大值為:%d\n",a[0]);
}
Ⅳ 如何在C++中創建一維動態數組
#include<iostream>
usingnamespacestd;
classPoint{
public:
Point(intx,inty):x(x),y(y){
cout<<"調用構造函數"<<endl;
}
Point():x(0),y(0){
cout<<"調用默認構造函數"<<endl;
}
~Point(){
cout<<"調用析構函數"<<endl;
}
intgetX()const{returnx;}
intgetY()const{returny;}
voidmove(intnewx,intnewy){
x=newx;
y=newy;
}
private:
intx,y;
};
intmain(){
Point*prt=newPoint[2];//創建對象數組
prt[0].move(3,4);//通過指針訪問數組成員
prt[1].move(4,3);
delete[]prt;
return0;
}
(5)創造一維數組擴展閱讀
C++動態二維矩陣
#include<iostream>
使用命名空間std;
intmain()
{
intd1,d2;
cout<<「輸入數組的行和列尺寸: 」;
cin>>d1>>d2;
int**m=新的int*[d1];
inti,j;
對於(i=0;i<d1;i++)
m[i]=newint[d2];
//m現在是d1xd2數組。
對於(i=0;i<d1;i++)
對於(j=0;j<d2;j++)
m[i][j]=(i+1)*10+j;
cout<<「回顯二維數組: 」;
for(i=0;i<d1;i++)
{
for(j=0;j<d2;j++)
cout<<m[i][j]<<「」;
cout<<endl;
}
對於(i=0;i<d1;
i++)delete[]m[i];
delete[]m;
系統(「暫停」);
返回0;
}
/*
輸入數組的行和列尺寸:
45
回聲二維數組:
1011121314
2021222324
3031323334
4041424344
*/
Ⅵ 如何在C++中創建一維動態數組
在C++語言中,二維動態數組主要使用指針的方法建立,以建立一個整數二維數組為例:
?
#include<iostream>#include<string>#include<malloc.h>using namespace std;int main(int argc,char **argv){///*int a[2][3]={{1,2,3},{4,5,6}};//cout<<sizeof(a+1)<<endl;*///int a=4;//int **pp;//pp=(int **)malloc(sizeof(int*)*a);//int aa[5][1]={1,2,3,4,5};//return 0;int column,row; cout<<"輸入二維數組的行數和列數"<<endl;cin>>row>>column;int **array;array = (int **)malloc(sizeof(int *)*row);for(int i=0;i!=row ; i++)array[i]=(int *) malloc(sizeof(int )*column);cout<<"輸入二維數組"<<endl;for(int j=0 ; j !=row ; j++){for(int k=0 ; k !=column ; k++) {cin>>array[j][k]; } }cout<<"輸入的二維數組為"<<endl;for( int j=0 ; j !=row ; j++ ){ for(int k=0 ; k !=column ; k++){cout<<array[j][k]<<" "; }cout<<endl; }//釋放空間 for(int i=0 ;i!=row;i++)free(array[i]);free(array);return 0;}動態創建一維數組
?
len;cout<<"輸入一維數組大小:"<<endl;cin>>len;int *p=new int[len];cout<<"輸入元素,元素之間以空格分隔!"<<endl;int val,i=0;for(i=0;i!=len;i++){cin>>val;p[i]=val;}cout<<"輸出一維數組:"<<endl;for(i=0;i!=len;i++){cout<<p[i]<<" ";}cout<<endl;動態分配二維數組
?
int main(int argc,char **argv){int column,row;cout<<"輸入二維數組的行數和列數"<<endl;cin>>row>>column;int **array;//array = (int **)malloc(sizeof(int *)*row);//方法一array=new int *[row];for(int i=0;i!=row ; i++)//array[i]=(int *) malloc(sizeof(int )*column);//方法一array[i]=new int [column];cout<<"輸入二維數組"<<endl;for(int j=0 ; j !=row ; j++){for(int k=0 ; k !=column ; k++) {cin>>array[j][k]; } }cout<<"輸入的二維數組為"<<endl;for( int j=0 ; j !=row ; j++ ){ for(int k=0 ; k !=column ; k++){cout<<array[j][k]<<" "; }cout<<endl; }//釋放空間 for(int i=0 ;i!=row;i++)free(array[i]);free(array);return 0;}C++中在結構體裡面動態創建數組,而且創建動態結構體數組
大家看一下這個例子就知道了!
?
int main(int argc, char* argv[]){int n,i,m,j;struct test{int *array;};test *testarray;cin>>n>>m;testarray=new test[m];for (i=0;i<m;i++){testarray[i].array=new int[n];}for (i=0;i<m;i++){for (j=0;j<n;j++){testarray[i].array[j]=i+j;}}for (i=0;i<m;i++){for (j=0;j<n;j++){cout<<testarray[i].array[j];}cout<<endl;}return 0;}Ⅶ C++動態創建一個一維數組 ,賦值 並顯示每個元素的值,最後釋放內存
#include<iostream>
using namespace std;
void main()
{
int*p,i;
p=new int[10];
for(i=0;i<10;i++)
p[i]=i;
for(i=0;i<10;i++)
cout<<p[i]<<" ";
cout<<endl;
delete[] p;
p=NULL;
}
Ⅷ 請問matlab 中如何創建並初始化一個一維數組
a = zeros(1,N)
建立一個1行N列的數組,初值為0,一般不需要用for的
Ⅸ 一維數組創建的幾種方法
import java.util.ArrayList;import java.util.List;public class ClientSocket{ public static void main(String[] args) throws Exception { List<Object> list = new ArrayList<Object>();//這里類型你自己指定 list.add("asd"); list.add(123); Object[] obj = new Object[list.size()]; obj = list.toArray(obj); }}
原理:ArrayList底層本身就是一個可變長度的數組,用ArrayList更方便,不用擔心溢出。
Ⅹ C#怎麼創建一維數組實例
兩種方法:
1、告訴他你要多大的數組
int[] arr=new int[100];
//100就是數組的長度
2、不告訴他有多大,直接告訴他數據組都有什麼值
int[] arr =new int[]{1,2,3,4,5,6,7,8,9};
或者 int[] arr={1,2,3,4,5,6,7,8};
常用的數組方法
using System;
using System.Collections;
public abstract class Array : ICloneable, IList, ICollection, IEnumerable
{
//判斷 Array 是否具有固定大小。
public bool IsFixedSize { get; }
//獲取 Array 元素的個數。
public int Length { get; }
//獲取 Array 的秩(維數)。
public int Rank { get; }
//實現的 IComparable 介面,在.Array 中搜索特定元素。
public static int BinarySearch(Array array, object value);
//實現的 IComparable<T>泛型介面,在 Array 中搜索特定元素。
public static int BinarySearch<T>(T[] array, T value);
//實現 IComparable 介面,在 Array 的某個范圍中搜索值。
public static int BinarySearch(Array array, int index,
int length, object value);
//實現的 IComparable<T>泛型介面,在 Array 中搜索值。
public static int BinarySearch<T>(T[] array,
int index, int length, T value);
//Array 設置為零、 false 或 null,具體取決於元素類型。
//System.Array 的淺表副本。
public object Clone();
//從第一個元素開始復制 Array 中的一系列元素
//到另一 Array 中(從第一個元素開始)。
public static void Copy(Array sourceArray,
Array destinationArray, int length);
//將一維 Array 的所有元素復制到指定的一維 Array 中。
public void CopyTo(Array array, int index);
//創建使用從零開始的索引、具有指定 Type 和維長的多維 Array。
public static Array CreateInstance(Type elementType,
params int[] lengths);
//返回 ArrayIEnumerator。
public IEnumerator GetEnumerator();
//獲取 Array 指定維中的元素數。
public int GetLength(int dimension);
//獲取一維 Array 中指定位置的值。
public object GetValue(int index);
//返回整個一維 Array 中第一個匹配項的索引。
public static int IndexOf(Array array, object value);
//返回整個.Array 中第一個匹配項的索引。
public static int IndexOf<T>(T[] array, T value);
//返回整個一維 Array 中最後一個匹配項的索引。
public static int LastIndexOf(Array array, object value);
//反轉整個一維 Array 中元素的順序。
public static void Reverse(Array array);
//設置給一維 Array 中指定位置的元素。
public void SetValue(object value, int index);
//對整個一維 Array 中的元素進行排序。
public static void Sort(Array array);
}